Page 1 of 1

About Question enthuware.ocpjp.v8.2.1671 :

Posted: Mon Mar 07, 2016 9:24 am
by javalass
This is the code:

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();  
And this is what the API for WatchKey says:
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.
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?

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?

Re: About Question enthuware.ocpjp.v8.2.1671 :

Posted: Mon Mar 07, 2016 10:41 am
by admin
javalass wrote:This is the code:

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();  
And this is what the API for WatchKey says:
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.
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?

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?
Yes, that is exactly what it implies. The control won't reach the end of the code until take() returns, and after that the status will be signalled.

HTH,
Paul.

Re: About Question enthuware.ocpjp.v8.2.1671 :

Posted: Mon Mar 07, 2016 11:10 am
by javalass
Arrrgh, tricky!! :o

But it makes sense, fair enough. Thanks for the super quick reply!

Re: About Question enthuware.ocpjp.v8.2.1671 :

Posted: Mon Mar 07, 2016 1:06 pm
by javalass
In fact, were we to assign the WatchKey returned by register() to key, the status of key would be, first, READY, then SIGNALLED.

Code: Select all

WatchKey key = path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE); //status: READY
watchService.take();
int i = key.pollEvents().size();//status: SIGNALLED