亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 5376 | 回復(fù): 2
打印 上一主題 下一主題

請教:關(guān)于RandomAccessFile阻塞ScheduledExecutorService的問題 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2013-11-13 20:41 |只看該作者 |倒序瀏覽
大家好,

我想實現(xiàn) 讀一個正在動態(tài)增長的日志 的功能,下面第一段代碼本夠用了,可惜 那個日志還會不定時自動 歸檔(滾動,也就是 *.log 變 *.log1 ,*.log1變 *.log2 ,類推)

已經(jīng)打開的 RandomAccessFile  仍然盯著最早關(guān)聯(lián)的文件,無法識別關(guān)聯(lián)到新生成的 *.log,

所以我修改了這段代碼,就是下面第二段代碼

新的問題是:

程序已運行到 randomFile.close(); 處就卡住 ,不能進行了

想知道原因,我對多線程了解不多,請大家指點。謝謝了

final RandomAccessFile randomFile = new RandomAccessFile(logFile, "rw");       
ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
                exec.scheduleWithFixedDelay(new Runnable() {
                        public void run() {
                                try
                                {
                                        RandomAccessFile randomFile = new RandomAccessFile(logFile, "rw");

                                        randomFile.seek(lastTimeFileSize);
                                        String tmp = "";


                                        while ((tmp = randomFile.readLine()) != null)
                                        {
                                                   //do something
                                        }
                                                       
                                       
                                }
                                catch (IOException e)
                                {
                                        throw new RuntimeException(e);
                                }



        ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
                exec.scheduleWithFixedDelay(new Runnable() {
                        public void run() {
                                try
                                {
                                        RandomAccessFile randomFile = new RandomAccessFile(logFile, "rw");

                                        randomFile.seek(lastTimeFileSize);
                                        String tmp = "";


                                        while ((tmp = randomFile.readLine()) != null)
                                        {
                                                   //do something
                                        }
                                        //程序會停止到這個位置,無法進行下去,沒有報任何異;蝈e誤
                                        //如果把 close() 給刪除掉,程序就可以正常往下運行了

                                        randomFile.close();                                                               
                                       
                                }
                                catch (IOException e)
                                {
                                        throw new RuntimeException(e);
                                }
                        }
                }, 0, 10, TimeUnit.SECONDS);

論壇徽章:
31
CU大;照
日期:2013-03-13 15:15:08CU大;照
日期:2013-05-20 10:46:18CU大;照
日期:2013-05-20 10:46:25CU大;照
日期:2013-05-20 10:46:31CU大;照
日期:2013-05-20 10:46:38CU大;照
日期:2013-05-20 10:46:44CU大;照
日期:2013-09-18 15:16:55CU大;照
日期:2013-09-18 15:18:22CU大牛徽章
日期:2013-09-18 15:18:43CU十二周年紀念徽章
日期:2013-10-24 15:41:34丑牛
日期:2013-12-01 10:11:07水瓶座
日期:2014-01-15 08:47:25
2 [報告]
發(fā)表于 2013-11-28 22:05 |只看該作者
我想你的問題應(yīng)該在這段代碼的synchronized里面。

  1.     /**
  2.      * Closes this random access file stream and releases any system
  3.      * resources associated with the stream. A closed random access
  4.      * file cannot perform input or output operations and cannot be
  5.      * reopened.
  6.      *
  7.      * <p> If this file has an associated channel then the channel is closed
  8.      * as well.
  9.      *
  10.      * @exception  IOException  if an I/O error occurs.
  11.      *
  12.      * @revised 1.4
  13.      * @spec JSR-51
  14.      */
  15.     public void close() throws IOException {
  16.         synchronized (closeLock) {
  17.             if (closed) {
  18.                 return;
  19.             }
  20.             closed = true;
  21.         }
  22.         if (channel != null) {
  23.             /*
  24.              * Decrement FD use count associated with the channel. The FD use
  25.              * count is incremented whenever a new channel is obtained from
  26.              * this stream.
  27.              */
  28.             fd.decrementAndGetUseCount();
  29.             channel.close();
  30.         }

  31.         /*
  32.          * Decrement FD use count associated with this stream.
  33.          * The count got incremented by FileDescriptor during its construction.
  34.          */
  35.         fd.decrementAndGetUseCount();
  36.         close0();
  37.     }
復(fù)制代碼

論壇徽章:
31
CU大;照
日期:2013-03-13 15:15:08CU大牛徽章
日期:2013-05-20 10:46:18CU大;照
日期:2013-05-20 10:46:25CU大;照
日期:2013-05-20 10:46:31CU大;照
日期:2013-05-20 10:46:38CU大;照
日期:2013-05-20 10:46:44CU大;照
日期:2013-09-18 15:16:55CU大;照
日期:2013-09-18 15:18:22CU大牛徽章
日期:2013-09-18 15:18:43CU十二周年紀念徽章
日期:2013-10-24 15:41:34丑牛
日期:2013-12-01 10:11:07水瓶座
日期:2014-01-15 08:47:25
3 [報告]
發(fā)表于 2013-11-28 22:10 |只看該作者
仔細想了下,問題不一定在close里面的同步哪兒。
我也沒有理解透,求指點。
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP