2

2 つのフォルダーがあり、それぞれに同じ WatchService で WatchKey を登録しました... 1 つのフォルダーで何かを変更すると、明らかに WatchEvent が発生します。しかし、イベントがあるとき、他のフォルダーの別のイベントを取得せずに、他のフォルダーの何かを変更したいと考えています。それは無限ループを引き起こすからです:(

このような私のコードは正しく動作しませんでした:

while (true) {
        try {
            key = watcher.take();
            keypath = (Path) key.watchable();
            list = key.pollEvents();
            for (WatchEvent<?> e : list) {
                if (e.kind() == OVERFLOW) {
                    System.err.println("OVERFLOW!");
                    break;
                } else {
                    file = new File(keypath.toString() + "\\" + e.context().toString());
                    System.out.println("FileChange detected: \"" + file + "\" [" + e.kind() + "]");
                    WatchKey other = getKey(getOtherFile(file));
                    other.cancel();
                    new Thread(new WatcherSync(this, file, sync)).start();
                    registerWatchKey((Path)other.watchable());
                }
            }
            if (!key.reset()) {
                System.err.println("Reset failed for WatchKey: " + keypath);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
4

0 に答える 0