How to stop Java WatchService whe it waiting for key?
I'm trying to stop my thread where WatchService is working. But how to do
that? My watchservice waiting for new update in folder there:
key = watchService.take();
I start my Thread there:
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();
}
My trying to stop:
private void stopButtonActionPerformed(ActionEvent e) {
try
{
if (watchService!=null)
{
key.cancel();
watchService.close();
}
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
When i'm stoping I get NullPointerException in key, when I'm just closing
watchService watchService.close() I get another exception:
ClosedWatchServiceException. How to close WathService without any
exceptions? Sorry for my bad english..
No comments:
Post a Comment