Which Operating Systems support native (inotify-like) file watching in Java

前端 未结 2 1109
没有蜡笔的小新
没有蜡笔的小新 2021-01-06 02:31

The JavaDoc for java.nio.file.WatchService states;

The implementation ... is intended to map directly on to the native file eve

2条回答
  •  忘掉有多难
    2021-01-06 02:56

    This should have probably been a comment, but it's too big to post it as such...

    I am looking at jdk-9 sources (could easily be searched in the jdk-8 repo as well), but here is some relevant to your question comments:

       /**
         * 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
    

    And for windows:

    /*
     * Win32 implementation of WatchService based on ReadDirectoryChangesW.
     */
    
    class WindowsWatchService extends AbstractWatchService
    

    And so on.. you can find all the available implementations under:

     jdk/src/java.base/{windows|unix|solaris|linux...}/classes/sun/nio/fs/
    

    As what OS actually supports this, it seems like that would require for you to look at the actual distro.

提交回复
热议问题