Is Java 7 WatchService Slow for Anyone Else?

时光总嘲笑我的痴心妄想 提交于 2019-11-26 13:01:25

问题


WatchService looks like a great technology but its been too slow to be useful on the OS X and Linux systems I\'ve tested on. To add insult to injury, it doesn\'t seem to get notified of all events either.

This is the case both with my own code and the canonical example from Oracle. (http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/essential/io/examples/WatchDir.java)

I recognize that the OS X OpenJDK port is unsure of this functionality (see https://wikis.oracle.com/display/OpenJDK/Mac+OS+X+Port+Project+Status)

Has anyone been using this in production with success?


回答1:


I have much better response times if I change

folder.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);

to

folder.register(watcher, new WatchEvent.Kind[]{StandardWatchEventKinds.ENTRY_MODIFY}, SensitivityWatchEventModifier.HIGH);



回答2:


JDK 7 does not yet have a native implementation of WatchService for MacOS. Rather than listening for native file system events, it uses the fallback sun.nio.fs.PollingWatchService, which periodically traverses the file system and checks the last modified timestamp of each file and subdirectory in the tree. I've also found it to be unusably slow.

There is a native implementation of WatchService for Mac:

http://code.google.com/p/barbarywatchservice/

I haven't tried to use it myself.



来源:https://stackoverflow.com/questions/9588737/is-java-7-watchservice-slow-for-anyone-else

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!