Code: Select all
Path path = Paths.get("C:/temp");
WatchService watchService = FileSystems.getDefault().newWatchService();
path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
WatchKey key = watchService.take();
There is no mention anywhere in the question that there were events for the WatchService to catch, so isn't the WatchKey ready until one such event happens?When initially created the key is said to be ready. When an event is detected then the key is signaled and queued so that it can be retrieved by invoking the watch service's poll or take methods. Once signalled, a key remains in this state until its reset method is invoked to return the key to the ready state.
Of course, one could argue that the variable key is only assigned a WatchKey when the take() method returns, so it is either unassigned or it points to a signalled key.
Is this what you meant in this question? There is a WatchKey in the ready state waiting for events, but it is only assigned to the variable once an event takes place, in which case the status will have changed to signalled?