title: WatchService not polling correctly
body: <p>I would like to poll a directory every 10 seconds to see if any files have been added or modified. If their are any changes within the 10 seconds I would like to have a set of all file paths that I can then pass to another method.    </p>
  
  <p><strong>Problem</strong></p>
  
  <p>When a file is added it is instantly recognized and the <code>addedFiles</code> method is called. Instead I would be expecting it to wait 10 seconds and to call the <code>addedFiles</code> method with multiple files that have been found.      </p>
  
  <p><strong>Example</strong><br>
  I've created a complete example that watches a directory. A thread then waits 5 seconds and copies 2000 files into the watched directory.<br>
  The expected behavior is for the <code>WatchService</code> to check for changes every 10 seconds. Instead it seems to be picking up changes instantly.    </p>
  
  <p><strong>Code</strong></p>
  
  <pre><code>import java.io.IOException;
  import java.io.PrintWriter;
  import java.nio.file.FileSystems;
  import java.nio.file.Path;
  import java.nio.file.Paths;
  import java.nio.file.StandardWatchEventKinds;
  import java.nio.file.WatchEvent;
  import java.nio.file.WatchKey;
  import java.nio.file.WatchService;
  import java.util.Collection;
  import java.util.HashSet;
  import java.util.concurrent.TimeUnit;
  
  public class DirectoryWatcherExample 
  {
      private static final int POLLING_TIME = 10;
  
      public static void main(final String args[]) throws InterruptedException, IOException
      {
          final Path directory = Paths.get("directory/to/be/watched");
  
          /**
           * Start a thread that will create 2000 files to the selected directory
           * This will occur after waiting 5 seconds.
           */
          new Thread(new Runnable()
          {
              @Override
              public void run() 
              {
                  try 
                  {
                      Thread.sleep(5000);         
                      System.out.println("Copying 2000 files to directory: " + directory);
                      for(int i = 0; i &lt; 2000; i++)
                      {
                          final PrintWriter writer = new PrintWriter(directory.resolve("test_file_" + i + ".txt").toFile(), "UTF-8");
                          writer.println("The first line");
                          writer.println("The second line");
                          writer.close();
                      }
                      System.out.println("Finished copying files to directory: " + directory);
                  } 
                  catch (final Exception e) 
                  {
                      e.printStackTrace();
                  } 
              }
          }).start();
  
          /**
           * Start the watch service polling every 10 seconds
           */
          new DirectoryWatcherExample().startWatchService(directory);
      }
  
      public void startWatchService(final Path directory) throws InterruptedException, IOException
      {
          final WatchService watchService = FileSystems.getDefault().newWatchService();
          directory.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY);
  
          while(true)
          {
              System.out.println("Start polling");
              final WatchKey key = watchService.poll(POLLING_TIME, TimeUnit.SECONDS);
              System.out.println("Finished polling and retrieved key");
  
              if(key != null)
              {
                  final Collection&lt;Path&gt; paths = new HashSet&lt;&gt;();
                  for (final WatchEvent&lt;?&gt; watchEvent : key.pollEvents())
                  {
                      final Path path = ((Path) key.watchable()).resolve((Path) watchEvent.context());
                      paths.add(path);
                      System.out.println("Path added: " + path);
                  }
  
                  // Do something with the paths
                  addedFiles(paths);
  
                  if (!key.reset())
                  {
                      break;
                  }   
              }
  
          }
      }
  
      // Unimplemented
      public void addedFiles(final Collection&lt;Path&gt; paths)
      {
  
      }
  }
  </code></pre>
  
  <p>What could be causing this? </p>