shutDown()
will only tell the executor to not accept any new tasks & cancels all tasks that are not runnuing yet. Tasks that are running are allowed to finish.
You are looping indefinitely in
while (!Thread.currentThread().isInterrupted()) {
key = waitForEvents(watcher, key);
}
because an InterruptedException
is not setting the interrupted flag.
If you change
try {
key = watcher.take();
} catch (InterruptedException e) {
executorService.shutdown();
} catch (ClosedWatchServiceException e) {
executorService.shutdown();
}
To
try {
key = watcher.take();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (ClosedWatchServiceException e) {
Thread.currentThread().interrupt();
}
and add executorService.shutdown();
after the while loop (or keep it where it is) you should have no problem.