Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.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_File_Listener - Fatal编程技术网

如果文件被修改,Java将收到通知

如果文件被修改,Java将收到通知,java,multithreading,file,listener,Java,Multithreading,File,Listener,我正在寻找一种方法,当某个文件被修改时获得通知。发生这种情况时,我希望调用某个方法,但在某些情况下,我还希望不调用该方法 我尝试了以下方法: class FileListener extends Thread { private Node n; private long timeStamp; public FileListener(Node n) { this.n = n; this.timeStamp = n.getFile().las

我正在寻找一种方法,当某个文件被修改时获得通知。发生这种情况时,我希望调用某个方法,但在某些情况下,我还希望不调用该方法

我尝试了以下方法:

class FileListener extends Thread {
    private Node n;
    private long timeStamp;

    public FileListener(Node n) {
        this.n = n;
        this.timeStamp = n.getFile().lastModified();
    }

    private boolean isModified() {
        long newStamp = n.getFile().lastModified();

        if (newStamp != timeStamp) {
            timeStamp = newStamp;
            return true;
        } else { 
            return false; 
    }

    public void run() {
        while(true) {
            if (isModified()) {
                n.setStatus(STATUS.MODIFIED);
            }
            try {
                Thread.sleep(1000);
            } catch(Exception e) {
                e.printStackTrace();
            }
    }
}
节点类包含对该文件的引用、状态(枚举)和对该文件的FileListener的引用。修改文件时,我希望状态更改为status.modified。但是,在某些情况下,节点引用的文件更改为新文件,我不希望它自动将状态更改为“已修改”。在这种情况下,我尝试了以下方法:

n.listener.interrupt(); //interrupts the listener
n.setListener(null); //sets listener to null
n.setFile(someNewFile); //Change the file in the node
//Introduce a new listener, which will look at the new file.
n.setListener(new FileListener(n)); 
n.listener.start(); // start the thread of the new listener
但我得到的是“Thread.sleep(1000)”引发的异常,因为睡眠被中断,当我检查状态时,它仍然被修改为status.modified


我做错什么了吗?

手表服务怎么样:

然后:

for (;;) {
  //wait for key to be signaled
  WatchKey key;
  try {
    key = watcher.take();
  } catch (InterruptedException x) {
    return;
  }

for (WatchEvent<?> event: key.pollEvents()) {
  WatchEvent.Kind<?> kind = event.kind();
  if (kind == OVERFLOW) {
      continue;
  }
...
}
(;;)的
{
//等待钥匙发出信号
监视键;
试一试{
key=watcher.take();
}捕捉(中断异常x){
返回;
}
for(WatchEvent事件:key.pollEvents()){
WatchEvent.Kind-Kind=event.Kind();
如果(种类==溢出){
继续;
}
...
}

看起来你好像做错了什么……真正的问题是你做错了什么?看看NIO的
WatchService
Thread.interrupt()
是终止线程的一种礼貌方式,而不是通知线程发生了变化。您确实想使用信号量。但正如Sotirios所说,
WatchService
是正确的解决方案。因此,apache vfs可能会重复这一点吗?
for (;;) {
  //wait for key to be signaled
  WatchKey key;
  try {
    key = watcher.take();
  } catch (InterruptedException x) {
    return;
  }

for (WatchEvent<?> event: key.pollEvents()) {
  WatchEvent.Kind<?> kind = event.kind();
  if (kind == OVERFLOW) {
      continue;
  }
...
}