java.nio.file.WatchService
的 JavaDoc 状态;
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 存储库中轻松搜索),但这里有一些与您的问题评论相关的内容:
/**
* 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/
由于操作系统实际支持此功能,因此您似乎需要查看实际的发行版。
关于java - 哪些操作系统支持在 Java 中监视 native (类似 inotify 的)文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43141838/