WatchKey#PollEvents() returning more events than expected

Umashankar Umashankar.Ningaiah at Sun.COM
Thu Jul 3 02:22:01 PDT 2008


Hi Tony,
 Please see my answers inline .

Subject:
WatchKey#PollEvents() returning more events than expected
From:
Tony Childs <tony.childs at gmail.com>
Date:
Wed, 02 Jul 2008 11:11:48 -0500

To:
nio-discuss at openjdk.java.net


I've been listening to the Google Talks presentation 
<http://video.google.com/videoplay?docid=-3975461488578314796> on NIO.2 
and reading through the PDF 
<http://openjdk.java.net/projects/nio/presentations/TS-5686.pdf> that 
Alan posted.  Upon testing code similar to that on page 22 of the PDF, I 
noticed unexpected (to me) behavior.  When watching a directory and then 
adding a space to a text file and then saving it, WatchKey#PollEvents() 
returns two ENTRY_MODIFY events that seem identical but are actually 
copies of each other.  Any idea why there would be two?

Multiple ENTRY_MODIFY events are normal. This is platform specific. 
There is a wording in WatchService spec about this. I am pasting it here 
for your reference
"many of the details on how events are detected, their timeliness, and 
whether their ordering is preserved is highly implementation specific. 
For example, when a file in a watched directory is modified then it may 
result in a single |ENTRY_MODIFY| 
<http://rain.ireland/%7Eab23780/private/jsr203/javadoc/java/nio/file/StandardWatchEventType.html#ENTRY_MODIFY> 
event in some implementations but several events in other implementations."

 Also, when I create a new file, in the watched directory, pollEvents() 
initially returns one ENTRY_CREATE event and then a subsequent call 
returns three more events: ENTRY_DELETE, ENTRY_CREATE, and 
ENTRY_MODIFY.  I'm confused as to why there are so many events for a 
seemly simple operation?

You should get only one ENTRY_CREATE event for this case. One reason why 
you are getting more events is, may be you are using an editor that 
creates a temp file and deletes it. You could print the context of the 
event and check the file which is causing the additional events.
Herewith I am attaching a small program for your reference. Please run 
the same and see if you still get the additional events.

Thanks,
Umashankar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/nio-discuss/attachments/20080703/64b89d30/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: WatchServiceEx.java
Type: text/x-java
Size: 2186 bytes
Desc: not available
Url : http://mail.openjdk.java.net/pipermail/nio-discuss/attachments/20080703/64b89d30/attachment.bin 


More information about the nio-discuss mailing list