【问题标题】:vfs2 DefaultFileMonitor thread terminates prematurelyvfs2 DefaultFileMonitor 线程提前终止
【发布时间】:2012-11-29 10:20:26
【问题描述】:

我无法保持 vfs2 的 DefaultFileMonitor 线程处于活动状态。在监视器对象启动后,执行的主线程会优雅地终止。我想知道为什么这个对象不会被“监控”,而是直接走到最后。 (以日志消息“exitting....”结尾)

public static void main(String[] args) {
    try {
        Options options = new Options();

        options.addOption("b", true, "path to the build file");
        options.addOption("d", true, "directory to watch");
        CommandLineParser parser = new PosixParser();
        CommandLine cmd = parser.parse(options, args);

        String dir = cmd.getOptionValue("d");
        String buildFile = cmd.getOptionValue("b");

        if(dir == null) {
            logger.error("No directory specified," +
                          " use [-d 'name_of_dir'] to specify one");
            return;
        }


        if(buildFile == null) {
            logger.error("No build file path specified," +
                          " use [-b 'path_to_build_file'] to specify one");
            return;
        }

        FileSystemManager fsManager = VFS.getManager();
        FileObject listendir = fsManager.resolveFile(dir);


        DefaultFileMonitor fm = new DefaultFileMonitor(new CustomFileListener(buildFile));
        fm.setRecursive(true);
        fm.addFile(listendir);
        fm.start();
    }catch(Exception e){
        logger.error("Exception ", e);
    }
    logger.info("exitting....");
}

【问题讨论】:

    标签: java multithreading vfs apache-commons-vfs file-monitoring


    【解决方案1】:

    DefaultFileMonitor 像守护线程一样工作,即,即使监控线程正在运行,虚拟机也会终止。一种解决方法是使用具有无限循环或其他类型的循环的非守护线程,它在您的控制之下。

    【讨论】:

      猜你喜欢
      • 2017-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-21
      • 1970-01-01
      相关资源
      最近更新 更多