WatchServicefrom JDK7 to monitor filesystem activity. I also wanted to demonstrate using it with an
As part of the process, I needed a graceful way to exit the program when the program completed. I thought that I would simply Google a solution, but I did not find one that met my needs.
So here is the use case I would like to have a WatchService running in its own thread and monitoring the filesystem. After a time period, I would like the service to shutdown gracefully.
I decided to use A
ScheduledExecutorServicewhich would start the WatchService using a
Callable<V>, and another
Runnablewhich was scheduled to shutdown the WatchService and the ScheduledExecutorService.
So here is how I did it.