Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用监视文件夹重新启动线程_Java_Multithreading_Key_Watchservice - Fatal编程技术网

Java 使用监视文件夹重新启动线程

Java 使用监视文件夹重新启动线程,java,multithreading,key,watchservice,Java,Multithreading,Key,Watchservice,我在设置文件中设置了正在监视文件夹的线程。如果我更改了设置,我想停止线程,并用实际要监视的文件夹启动新线程。此代码运行“正常”,但收到一些错误消息 public static void resetWatch() throws FileNotFoundException, IOException, InterruptedException { BufferedReader setup = new BufferedReader(new FileReader(new File("setup\\

我在设置文件中设置了正在监视文件夹的线程。如果我更改了设置,我想停止线程,并用实际要监视的文件夹启动新线程。此代码运行“正常”,但收到一些错误消息

public static void resetWatch() throws FileNotFoundException, IOException, InterruptedException {
    BufferedReader setup = new BufferedReader(new FileReader(new File("setup\\setup.dat")));
    String DirtoWatch = setup.readLine();

    Path toWatch = Paths.get(DirtoWatch);
    if(toWatch == null) {
        throw new UnsupportedOperationException("Directory not found");
    }

    // make a new watch service that we can register interest in
    // directories and files with.
    myWatcher = toWatch.getFileSystem().newWatchService();

    // start the file watcher thread below
    fileWatcher = new Watcher(myWatcher, toWatch);

    th = new Thread(fileWatcher, "FileWatcher");
    th.start();
    System.out.println("Monitoring " + DirtoWatch + " for changes...");
    // register a file
    toWatch.register(myWatcher, ENTRY_CREATE);
    th.join();
}
观察者类

public class Watcher implements Runnable {

    private WatchService myWatcher;
    public Path path;
    private WatchKey key;

    public Watcher(WatchService myWatcher, Path path) {
        this.myWatcher = myWatcher;
        this.path = path;
    }

    @Override
    public void run() {
        String evCon;

        try {
            // get the first event before looping
            key = myWatcher.take();
            while(key != null) {

                for (WatchEvent event : key.pollEvents()) {
                    evCon =  event.context().toString();
                    System.out.println("New file: " + evCon);
                    Thread.sleep(500);
                    folder_changed(path.toString() + "/" + evCon);
                }
                key.reset();
                key = myWatcher.take();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (IOException ex) {
            Logger.getLogger(Watcher.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("Stopping thread");
    }

    public void stopThread() {
        try {
            System.out.println("Closing the ws");
            myWatcher.close();
            if(key!=null) {
                key.cancel();  
            }
            Thread.currentThread().interrupt();
        } catch (IOException | ClosedWatchServiceException exc) { System.out.println("Closing thread exception"); }
    }
}
如果我更改设置,我会更新设置文件,然后尝试关闭watcher并再次运行线程:

try{
        fileWatcher.stopThread();
        myWatcher.close();
    }
    catch(ClosedWatchServiceException | IOException ex) { System.out.println("Watch Service exc. "); } 

    try {
        resetWatch();
    } catch (IOException | InterruptedException ex) { System.out.println("Recall resetWatch exc. "); }
然后它“正在运行”,但我得到以下异常,指向watch键:

Monitoring ..path.. for changes...
Closing the ws
Monitoring ..path.. for changes...
Exception in thread "FileWatcher" java.nio.file.ClosedWatchServiceException
Recall resetWatch exc. 
    at sun.nio.fs.AbstractWatchService.checkOpen(AbstractWatchService.java:80)
    at sun.nio.fs.AbstractWatchService.checkKey(AbstractWatchService.java:92)
    at sun.nio.fs.AbstractWatchService.take(AbstractWatchService.java:119)
    at jstockcheck.Watcher.run(Watcher.java:40)
    at java.lang.Thread.run(Thread.java:748)

如何避免这种错误,有什么建议吗?谢谢

可以随时调用
close
方法来关闭服务,导致等待检索密钥的任何线程抛出
ClosedWatchServiceException

ClosedWatchServiceException
-如果此监视服务已关闭,或在等待下一个按键时已关闭

在您的情况下,我认为,这只是在调用
take
时捕获异常并静默处理它的问题,例如:

try {
    key = myWatcher.take()
} catch (ClosedWatchServiceException e) {
    continue;
}
这是根据:

您是如何解决此问题的。我正在寻求类似的帮助,我想重新启动我的Java程序,我可以使用您使用的相同方法吗?这是我关于重新启动脚本的问题-您是如何解决此问题的。我正在寻求类似的帮助,我想重新启动我的Java程序,我可以使用您使用的相同方法吗?这是我关于重新启动脚本的问题-stackoverflow.com/questions/66169748/…–@Aldic,可能有助于解决此问题。我正在努力理解你的决心。