About Question enthuware.ocpjp.v8.2.1671 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
javalass
Posts: 54
Joined: Thu Mar 03, 2016 1:26 pm
Contact:

About Question enthuware.ocpjp.v8.2.1671 :

Post 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?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

javalass
Posts: 54
Joined: Thu Mar 03, 2016 1:26 pm
Contact:

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

Post by javalass »

Arrrgh, tricky!! :o

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

javalass
Posts: 54
Joined: Thu Mar 03, 2016 1:26 pm
Contact:

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

Post 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

Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests