当文件结构更改时。我不断为更改的每个文件获得println
s。如何使我在 10 秒内只能获得一个println
进行多次更改?
try {
WatchService watchService = FileSystems.getDefault().newWatchService();
Path path = new File("C:\Users\myuser\Desktop\TestFolder").toPath();
path.register(
watchService,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE,
StandardWatchEventKinds.ENTRY_MODIFY);
WatchKey key;
while ((key = watchService.take()) != null) {
for (WatchEvent<?> event : key.pollEvents()) {
System.out.println(
"Event kind:" + event.kind() + ". File affected: " + event.context() + ".");
System.out.println("Something changed!");
Thread.sleep(10000);
System.out.println("Resuming..");
break;
}
key.reset();
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
好吧,你可以做一些类似的事情,使用WatchService.poll(long timeout, TimeUnit unit)
和Thread.sleep(10000)
组合。
看看这个
/**
* Method used for tracking corresponding events( create,delete, modify )
* for a given directory every 10 seconds.
* @throws InterruptedException
*/
private void monitoringWithTenSecondsLate() throws InterruptedException {
WatchKey watchKey = null;
while (true) {
try {
//poll everything every 10 seconds
watchKey = watchService.poll(10, TimeUnit.SECONDS);
for (WatchEvent<?> watchEvent : watchKey.pollEvents()) {
WatchEvent.Kind<?> kind = watchEvent.kind();
System.out.println("Event occured for " + watchEvent.context().toString() + " " + kind.toString());
}
} catch (InterruptedException ex) {
Logger.getLogger(WatchServiceExample10SecondsLate.class.getName()).log(Level.SEVERE, null, ex);
}
//don't reset watch key for 10 seconds
Thread.sleep(10000);
boolean reset = watchKey.reset();
if (!reset) {
break;
}
}
}
完整的代码可以从这里获得:Github代码的监视服务10秒示例。
我只是轮询所有键并连接结果。
大致如下:
while ((key = watchService.take()) != null) {
StringJoiner joiner = new StringJoiner(",");
for (WatchEvent<?> event : key.pollEvents()) {
joiner.add(event.getContext().toString())
}
key.reset();
Thread.sleep(10000);
System.out.println("Resuming ...");
System.out.println("Something has changed! Affected files:" + joiner.toString())
}
所以现在,据我了解,线程休眠时的事件会被排队,仍然会被处理。我想你想要的,从你写的内容来看,仍然是处理这些事件,但只打印 10 秒后事件的总列表。在这种情况下,处理事件的 Thread.sleep(( 不是一个好的解决方案。
您可以做的是为观察程序实现一个线程,例如,以线程安全的方式将其注册的每个事件添加到变量中,以便您的主线程在 10 秒后访问。因此,在您的主线程中,您将有一个循环,该循环将休眠 10 秒,并在醒来时检索此变量,将其打印出来并重置它。
它会是这样的:
private Object lock = new Object(); // defined in your watcher class
private String events = new String() // also defined in your class
thread = new Thread(listeningFunc);
thread.start()
while(True) {
Thread.sleep(10000);
syncronized(lock) {
System.out.println(events);
events = new String()
}
}
// With this function defined in your class to run in your new thread
listeningFunc() {
path.register(
watchService,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE,
StandardWatchEventKinds.ENTRY_MODIFY);
WatchKey key;
while((key = watchService.take()) != null) {
for (WatchEvent<?> event : key.pollEvents()) {
syncronized(lock) {
events += "Event kind:" + event.kind() + ". File affected: " + event.context() + ".n"
}
}
key.reset()
}
}