为什么使用WatchService延迟检测文件更改?(Java)

Nir*_*ian 2 java watchservice

我有一个应用程序,其中将文件添加到目录中时,WatchService会检测到该文件,并将该文件添加到文件列表中以进行进一步处理。这是我的代码

 public void run() {

    /*
     * Goes in an infinite loop
     */
     while(!finished) {

     /*
      *  Get a watch key, poll() returns a queued key 
      *  if no queued key, this method waits until the specified time.
      */
     WatchKey key;
     try {
             key = watcher.poll(eofDelay,TimeUnit.MILLISECONDS);
      } catch (InterruptedException x) {
          return;
      }

     Path dir = keys.get(key);

     if (dir == null) {
         continue;
      }

     Path child=null;

         /*
          * Fetching the list of watch events from
          * pollEvents(). There are four possible events
          */

         for (WatchEvent<?> event: key.pollEvents()) {
            WatchEvent.Kind kind = event.kind();

            /*
             * Overflow indicates that events 
             * might have been lost or discarded
             */
             if (kind == OVERFLOW) {
                 continue;
             }


             WatchEvent<Path> ev = cast(event);

             /*
              * Filename is the context of the event
              */
             Path name = ev.context();

             /*
              * Resolves the name of the file to a path
              */
              child = dir.resolve(name);

             /*
              *  If directory is created, and watching recursively, then
              * register it and its sub-directories
              */
             if (nested && (kind == ENTRY_CREATE)) {
                 try {
                     if (Files.isDirectory(child, NOFOLLOW_LINKS)) {
                         registerAll(child);
                     }
                 } catch (IOException x) {

                 }
             }
         }

         File file = child.toFile();

         /*
          * Only add the file if there is no wild card 
          * or it matches the specified wild card 
          */
         if (matcher == null || matcher.matches(file.toPath().getFileName())) {
             fileList.add(file);
         }
     /*
      * Key is reset so that it can receive further
      * events 
      */

         boolean valid = key.reset();
         if (!valid) {
             keys.remove(key);

            /*
             * If key is no longer valid and empty,
             * exit the loop
             */
             if (keys.isEmpty()) {
                continue;
             }
         }

     }
 }
Run Code Online (Sandbox Code Playgroud)

这段代码可以正常工作,但是我正在设计一个高性能的应用程序,它可以非常快速地处理文件中的数据。因此,这里的问题是检测文件所用时间不一致。例如,最初在目录中有一些文件,由应用程序处理,现在添加新文件时,检测该文件需要4-5秒,有时甚至需要2秒或20ms等。我的eofDelay值为10ms。这种不一致的原因是什么?有没有办法增强这种实现?还是任何其他可用于目录更改的高效库?我希望检测文件所需的时间最少且保持一致,花费一秒钟以上的时间非常昂贵。在这方面的任何帮助将不胜感激。:)

sev*_*ens 5

通过在文件夹中添加敏感度标志,您可能会获得更快的结果(请参见下文)。

// copied from http://stackoverflow.com/questions/9588737/is-java-7-watchservice-slow-for-anyone-else
folder.register(watcher, new WatchEvent.Kind[]{StandardWatchEventKinds.ENTRY_MODIFY}, SensitivityWatchEventModifier.HIGH); 
Run Code Online (Sandbox Code Playgroud)

但是,您仍然会受到底层操作系统的支配。我见过的大多数文件监视应用程序在添加文件和拾取文件之间都有几秒钟的延迟。您在应用程序中看到了正常的延迟时间。

如果您的应用程序必须在几毫秒内响应添加的一些文件,则不应使用Java(NIO或其他方式),而应使用C / C ++。这将增加代码的复杂性。