请教:关于RandomAccessFile阻塞ScheduledExecutorService的问题

大家好,

我想实现 读一个正在动态增长的日志 的功能,下面第一段代码本够用了,可惜 那个日志还会不定时自动 归档(滚动,也就是 .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);
不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^