WatchService

Dr Heinz M. Kabutz heinz at javaspecialists.eu
Sun Oct 21 23:42:30 PDT 2012


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



More information about the macosx-port-dev mailing list