我试图停止我的线程在哪里WatchService工作。但是怎么做呢?我WatchService在那里等待文件夹中的新更新:
key = watchService.take();
我在那里开始我的线程:
private void startButtonActionPerformed(ActionEvent e) {
stateLabel.setText("monitoring...");
try {
watchService = FileSystems.getDefault().newWatchService();
} catch (IOException e1) {
e1.printStackTrace();
}
watchThread = new WatchThread(watchService, key);
Thread t = new Thread(watchThread);
t.start();
}
我试图停止:
private void stopButtonActionPerformed(ActionEvent e) {
try {
if (watchService!=null) {
key.cancel();
watchService.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
当我尝试执行停止时,我得到NullPointerException了关键。当我刚刚用 关闭 watchService 时watchService.close(),我得到另一个异常ClosedWatchServiceException:
如何关闭一个WatchService没有任何异常?对不起,我的英语不好..