About Question enthuware.ocpjp.v8.2.1673 :
Moderator: admin
-
- Posts: 3
- Joined: Tue Feb 28, 2017 12:18 am
- Contact:
About Question enthuware.ocpjp.v8.2.1673 :
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
Online
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1673 :
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.
-
- Posts: 1
- Joined: Tue Aug 08, 2017 7:07 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1673 :
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: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.
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);
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.
Online
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1673 :
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.
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.
Who is online
Users browsing this forum: admin and 33 guests