WatchService and SwingWorker: how to do it correctly?

Because your background thread is devoted entirely to watching, take() is the right choice. It effectively hides the platform dependent implementation, which may either forward or poll. One of the poll() methods would be appropriate if, for example, your background thread also needed to examine other queues in series with the WatchService.

Addendum: Because the WatchKey has state, it should probably not be forwarded to process(). The context() of a WatchEvent is a “relative path between the directory registered with the watch service and the entry that is created, deleted, or modified.” One of the resolve() methods should work if the directories share a common root.

Leave a Comment