Fwd: WatchService

Michael McMahon michael.x.mcmahon at oracle.com
Mon Oct 22 00:52:13 PDT 2012


Forwarding to nio-dev


-------- Original Message --------
Subject: 	WatchService
Date: 	Mon, 22 Oct 2012 08:42:30 +0200
From: 	Dr Heinz M. Kabutz <heinz at javaspecialists.eu>
Organization: 	JavaSpecialists.eu
To: 	macosx-port-dev at openjdk.java.net



Hi all,

are there any plans to implement a native WatchService implementation
for the Mac OS X file system?

I just hope that the Mac OS X version of OpenJDK will eventually have
the system specific extensions built in.  For example, here is a file
watcher class:

import java.io.*;
import java.nio.file.*;

public class FileWatcher {
    public static void main(String[] args) throws IOException,
            InterruptedException {
        WatchService watcher = FileSystems.getDefault().newWatchService();
        Path opt = FileSystems.getDefault().getPath(".");
        WatchKey key = opt.register(watcher,
                StandardWatchEventKinds.ENTRY_CREATE,
                StandardWatchEventKinds.ENTRY_DELETE,
                StandardWatchEventKinds.ENTRY_MODIFY);
        while (true) {
            key = watcher.take();
            for (WatchEvent<?> ev : key.pollEvents()) {
                System.out.println(ev.context());
                System.out.println(ev.count());
            }
            key.reset();
        }
    }
}

Works brilliantly on Windows and Linux, but on Mac, it creates an
unnamed thread that polls the directory every 10 seconds!

"Thread-0" daemon prio=5 tid=0x00007ff7728cb000 nid=0x5203 waiting on
condition [0x000000016532d000]
   java.lang.Thread.State: TIMED_WAITING (parking)
    at sun.misc.Unsafe.park(Native Method)
    - parking to wait for  <0x00000001449c3e50> (a
java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    at
java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:226)
    at
java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2082)
    at
java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1090)
    at
java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:807)
    at
java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1043)
    at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1103)
    at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

Regards

Heinz
-- 
Dr Heinz M. Kabutz (PhD CompSci)
Author of "The Java(tm) Specialists' Newsletter"
Sun Java Champion
IEEE Certified Software Development Professional
http://www.javaspecialists.eu
Tel: +30 69 75 595 262
Skype: kabutz




-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/nio-dev/attachments/20121022/34e264fb/attachment.html 


More information about the nio-dev mailing list