Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop Watching A Directory for Changes (cleanup)

I implemented a directory watcher following the java tutorial: https://blogs.oracle.com/thejavatutorials/entry/watching_a_directory_for_changes

I have it on a separate thread that updates a JList. Everything seems to be working fine, what I wanted to know is: Do I need to do some type of cleanup somewhere and what would be the best way to do so if that's the case. Because that thread is basically waiting the whole time just in case something happens, after that it goes back to waiting for the next change.

Thanks for the help.

like image 333
user1294431 Avatar asked Nov 12 '22 19:11

user1294431


1 Answers

You should close the WatchService.

http://openjdk.java.net/projects/nio/javadoc/java/nio/file/WatchService.html#close()

Obviously, if you are going to watch the same path(s) for the duration of your program, you can just exit and rely on OS to clean up.

like image 70
Igor Nazarenko Avatar answered Nov 15 '22 10:11

Igor Nazarenko