JavaDoc for java.nio.file.WatchService状态;
The implementation … is
intended to map directly on to the native file event notification
facility where available, or to use a primitive mechanism, such as
polling, when a native facility is not available.
我认为这意味着它将尝试一种轻量级的本机机制(如Linux上的inotify),如果不能,则进行轮询.
那是对的吗?
哪些操作系统可能或不可能提供这样的设施? Linux的发行版级别真的很有用,或者我可以假设如果JVM在* unix上运行,它会受到支持吗?
解决方法:
这应该是一个评论,但它太大了,不能发布它…
我正在查看jdk-9源代码(也可以在jdk-8 repo中轻松搜索),但这里有一些与你的问题评论有关:
/**
* Linux implementation of WatchService based on inotify.
*
* In summary a background thread polls inotify plus a socket used for the wakeup
* mechanism. Requests to add or remove a watch, or close the watch service,
* cause the thread to wakeup and process the request. Events are processed
* by the thread which causes it to signal/queue the corresponding watch keys.
*/
class LinuxWatchService extends AbstractWatchService
对于Windows:
/*
* Win32 implementation of WatchService based on ReadDirectoryChangesW.
*/
class WindowsWatchService extends AbstractWatchService
等等..您可以在以下位置找到所有可用的实现:
jdk/src/java.base/{windows|unix|solaris|linux...}/classes/sun/nio/fs/
正如操作系统实际上支持这一点,似乎需要您查看实际的发行版.