【问题标题】:JVM closes while using a continous Thread for no reasonJVM在使用连续线程时无缘无故关闭
【发布时间】:2016-08-06 12:43:26
【问题描述】:

我的程序出现问题。 我正在尝试按结尾对下载的文件进行排序。到目前为止,我已经有了观察给定文件路径并在HashSet 中列出这些文件的结构。 我现在的问题是,程序会继续运行几秒钟,然后以退出代码 0 结束,所以一切都应该没问题。

public class WatchDir {

    protected HashSet<File> hashSetOfFiles;
    protected String filePath = "";

    public WatchDir() {

        chooseFilePath();
        if (filePath.isEmpty())
            return;

        listAnfangFiles();

        new Thread() {
            public void run() {
                listNewFiles();
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            };
        }.start();
    }

    private void listNewFiles() {

        File file = new File(filePath);

        for (File f : file.listFiles()) {
            if (hashSetOfFiles.add(f)) {
                newFileFound(f);
            }
        }
    }

    private void newFileFound(File f) {

        // Hier kommen alle neuen Dateien an
        System.out.println(f.getName());
    }

    private void listAnfangFiles() {

        hashSetOfFiles = new HashSet<File>();
        File f = new File(filePath);

        for (File ff : f.listFiles()) {
            hashSetOfFiles.add(ff);
        }
    }

    private void chooseFilePath() {
        //
        // JFileChooser chooser = new JFileChooser();
        // chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        //
        // if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
        // filePath = chooser.getSelectedFile().getAbsolutePath();

        filePath = "C:\\Users\\maurice\\Desktop\\Test";
    }

    public static void main(String[] args) {
        new WatchDir();
    }
}

【问题讨论】:

    标签: java multithreading file timer directory


    【解决方案1】:
        boolean wantToContinue = true;
        new Thread() {
        public void run() {
    
            while (wantToContinue){
               listNewFiles();
               try {
                   sleep(1000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
            }
    
        };
    }.start();
    

    使用布尔变量。因此,您可以从外部控制动作。给出真实的条件不是好的做法。

    【讨论】:

      【解决方案2】:
       new Thread() {
              public void run() {
                  listNewFiles();
                  try {
                      sleep(1000);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
              };
          }.start();
      

      这不是一个连续的线程。 在run() 方法中,您没有允许线程继续连续运行的循环条件。
      所以sleep(1000); 播放一次,它是你的run() 方法中最后播放的指令。返回后,run()方法已经终止,线程也因此结束。


      编辑:一个连续线程的简单解决方案

      例如,这段代码允许线程永不停止,而是一个进程终止:

       new Thread() {
              public void run() {
      
                  while (true){
                     listNewFiles();
                     try {
                         sleep(1000);
                     } catch (InterruptedException e) {
                         e.printStackTrace();
                     }
                  }
      
              };
          }.start();
      

      您可以有其他条件来停止循环(延迟、日期或时间、状态、按下的键等...)。要允许它,您应该将它放在 while() 条件或 if (condition) break 块中。

      【讨论】:

        猜你喜欢
        • 2016-08-22
        • 2018-08-17
        • 1970-01-01
        • 1970-01-01
        • 2015-03-01
        • 1970-01-01
        • 2014-02-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多