Issue with WatchService on Mac OS 10.9.1 using Oracle JDK 1.7.0_51

Christian Schlichtherle christian at schlichtherle.de
Wed Jan 15 09:30:08 PST 2014


Hi everyone,

I have found what I think is a concurrency issue with the WatchService implementation for Mac OS X. I am using Oracle JDK 1.7.0_51 on Mac OS 10.9.1.

Starting with a Path referring to an existing directory, I do the equivalent of this:

final Path directory = Paths.get(„.“); // just for the sample
final WatchService service = directory.getFileSystem().newWatchService();
directory.register(service, ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE);
new Thread() {
    @Override public void run() {
        try {
            while (true) {
                final WatchKey wk;
                try {
                    wk = service.take();
                } catch (ClosedWatchServiceException ex) {
                    break;
                }
                assert wk.isValid();
                for (final WatchEvent<?> event : wk.pollEvents()) {
                    ...
                }
                if (!wk.reset()) break;
            }
        } catch (IOException ex) {
            ...
        }
    }
}.start();
Thread.sleep(250);
try (BufferedWriter w = Files.newBufferedWriter(directory.resolve(„foo“), Charset.forName(„US-ASCII“), StandardOpenOption.APPEND)) {
    w.write(„Hello world!“);
}
…

Now if I comment out the call to Thread.sleep(250), then the background thread may miss the modification of the file „foo" on the foreground thread approx. 50% of the time.

So I wonder if I am using this API wrong or is this a bug?

Furthermore, I found the Mac OS X implementation of the WatchService to be a PollingWatchService. Is there a way to configure the polling interval? It seems to be about ten seconds or so, figured from doing trial-and-error tests.

With best regards,
Christian Schlichtherle

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 495 bytes
Desc: Message signed with OpenPGP using GPGMail
Url : http://mail.openjdk.java.net/pipermail/nio-discuss/attachments/20140115/2f332c22/signature.asc 


More information about the nio-discuss mailing list