title: WatchService uses 100% of CPU on CentOS
body: <p>I am using a <code>WatchService</code> in my application. When I run my application on a <code>Windows</code> environment, the application uses less than 1% of the <code>CPU</code>. When the same application is run on my <code>Linux</code> server, it uses 100% of the <code>CPU</code>. When the <code>WatchService</code> thread is disabled, the <code>CPU</code> is back to normal.</p>
  
  <p>I am using <code>CentOS 5.9</code> with <code>OpenJDK-1.7.0_x86_64</code>.</p>
  
  <p>Here is the thread:</p>
  
  <pre><code>private static void startDirectoryWatcher() {
      if (thWatcherM == null) {
          thWatcherM = new Thread(new Runnable() {
              @Override
              public void run() {
                  if (mediaMode == MediaMode.Directory &amp;&amp; !exit) {
  
                      File music = new File(path);
  
                      WatchService watcherM = null;
  
                      watcherM = music.toPath().getFileSystem().newWatchService();
                      music.toPath().register(watcherM, StandardWatchEventKinds.ENTRY_CREATE);
  
                      while (!exit) {
                          Thread.sleep(50);
                          if (watcherM != null) {
                              WatchKey watchKey = watcherM.take();
  
                              List&lt;WatchEvent&lt;?&gt;&gt; events = watchKey
                                      .pollEvents();
                              for (WatchEvent&lt;?&gt; event : events) {
                                  if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) {
                                      System.out.println(event.context().toString());
                                  }
                              }
  
                              if (!watchKey.reset()) {
                                  break;
                              }
                          }
                      }
  
                      if (watcherM != null) {
                          watcherM.close();
                      }
  
                  }
              }
          });
          thWatcherM.setName("Dir-Watcher-M");
          thWatcherM.start();
      }
  }
  </code></pre>
  
  <p>Why is it using <strong>100%</strong> of the <code>CPU</code>?</p>