About Question enthuware.ocpjp.v8.2.1673 :

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

Moderator: admin

Post Reply
gubble
Posts: 3
Joined: Tue Feb 28, 2017 12:18 am
Contact:

About Question enthuware.ocpjp.v8.2.1673 :

Post by gubble »

Isn't it possible, considering the lag of WatchService, that the key that we get contains both the ENTRY_CREATE and ENTRY_DELETE events? Especially if the file is cerated and deleted in a very short time frame

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

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

Post by admin »

No, until you reset the key, you will not get the second event.
If you like our products and services, please help us by posting your review here.

dcolvin
Posts: 1
Joined: Tue Aug 08, 2017 7:07 pm
Contact:

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

Post by dcolvin »

To the contrary, I believe it is indeed possible for the key to get both events. Per the WatchKey Javadocs, "Events are retrieved by invoking the key's pollEvents method. This method retrieves and removes all events accumulated for the object."

To show how both events could accumulate, I tried adding the following code just before the while loop:

Code: Select all

Path file = Paths.get(directoryPath, "testFile.txt");
Files.createFile(file);
Files.delete(file);
I get the output:
true
sun.nio.fs.AbstractWatchKey$Event@7ea987ac
sun.nio.fs.AbstractWatchKey$Event@12a3a380

and for clarity, changing println(watchEvent) to println(kind + " " + watchEvent):
true
ENTRY_CREATE sun.nio.fs.AbstractWatchKey$Event@7ea987ac
ENTRY_DELETE sun.nio.fs.AbstractWatchKey$Event@12a3a380

confirming the two events gotten.

One more consideration is that a file creation could possibly result in one or more ENTRY_MODIFY events in addition to the ENTRY_CREATE event, depending on how the file is created. For example, when I instead create a file in the watched directory at the command line (e.g., "echo > testFile" or "copy file1 file2") sometimes I get:
true
ENTRY_CREATE sun.nio.fs.AbstractWatchKey$Event@7ea987ac
ENTRY_MODIFY sun.nio.fs.AbstractWatchKey$Event@12a3a380

So in this way as well, more than one event could be received even without calling reset() on the key.

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

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

Post by admin »

You are right.
The issue is actually with the event that happens after pollEvents has been called but reset has not yet been called. During this time, if you call pollEvent again, you will get that event but if you call reset, then that event will be lost. So the point is that there is a small duration between pollEvents and reset during which an event can get lost.

In the given question, it is possible to get two events if they happen very quickly (I have updated the problem statement to make that clear) but in normal course, the second event will not be printed.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 36 guests