title: Event loop in java
body: <p>I'm actually trying to write an event loop for Nashorn (java 8) so that callbacks from asynchronous operations (threads i launch to, for example, connect to remote services or do long-running calculations) will be put in a queue and executed in sequence (not in parallel). I'm doing it by placing the callback functions on a ConcurrentLinkedQueue and using a ScheduledExecutorService as the loop that checks the queue for a callback to execute. </p>
  
  <p>Works fine, but my questions are: </p>
  
  <p>1) how short an interval can I use without dragging my CPU? I will have multiple of these running and they must be independent of one another. Thus there may be 50 threads all running their own "event loop". This executor attempts to run my runnable every 10ms, for example....</p>
  
  <pre><code>Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(&lt;cbRunnable&gt;, 0, 10, TimeUnit.MILLISECONDS);
  </code></pre>
  
  <p>2) Does this approach have an advantage over:</p>
  
  <pre><code>while (true) {
     // check queue and execute any callback...
  }
  </code></pre>
  
  <p>3) Is there a better way?</p>