title: While loop with small Thread.Sleep vs. Java Timer?
body: <p>I have a program that needs to execute a process at a very precise time. The way I have currently approached it is:</p>
  
  <pre><code>while( ( exTime - System.currentTimeMillis() ) &gt; randomNum )
  {
      try
      {
          Thread.sleep(0, 5000); //Sleep for 5000 nanoseconds, or 0.000005 seconds
      }
      catch (Exception e)
      {
          e.printStackTrace();
      }
  }
  //Code to be executed after time is over.
  </code></pre>
  
  <p>I know it is also possible to use a Java timer for this. </p>
  
  <p>My question is what is the most efficient, and what is most accurate? I am running over 100 threads with the while-loop. Will using a timer save more resources and be more accurate?</p>