免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5350 | 回复: 2
打印 上一主题 下一主题

请教:关于RandomAccessFile阻塞ScheduledExecutorService的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-11-13 20:41 |只看该作者 |倒序浏览
大家好,

我想实现 读一个正在动态增长的日志 的功能,下面第一段代码本够用了,可惜 那个日志还会不定时自动 归档(滚动,也就是 *.log 变 *.log1 ,*.log1变 *.log2 ,类推)

已经打开的 RandomAccessFile  仍然盯着最早关联的文件,无法识别关联到新生成的 *.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
                                        }
                                        //程序会停止到这个位置,无法进行下去,没有报任何异常或错误
                                        //如果把 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 [报告]
发表于 2013-11-28 22:05 |只看该作者
我想你的问题应该在这段代码的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.     }
复制代码

论坛徽章:
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 [报告]
发表于 2013-11-28 22:10 |只看该作者
仔细想了下,问题不一定在close里面的同步哪儿。
我也没有理解透,求指点。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP