title: Performance considerations for Thread.sleep() and schedulerExecutorService.scheduleWithFixedDelay
body: <p>I am trying to write a daemon thread for polling which will poll every "3" seconds. I've seen 3 approaches. I am not sure which one to go for ? I want to know the performance considerations. Which one could be consistant and will take smaller memory chunk ?</p>
  
  <blockquote>
    <p>Approach 1 :  Using the TimerTask  ( Not planning to use this )</p>
  </blockquote>
  
  <pre><code>    new Timer().scheduleAtFixedRate(task, firstTime, period)
  </code></pre>
  
  <blockquote>
    <p>Approach 2 : Using ScheduledExecutorService</p>
    
    <p>Approach 3 : Using Thread.sleep() ....</p>
  </blockquote>
  
  <p>If I boil down to code its something like this : </p>
  
  <blockquote>
    <p>Approach #2</p>
  </blockquote>
  
  <pre><code>public class Test {
  
      static TimerTask timerTask = new TimerTask() {
          @Override
          public void run() {
                // Do polling &amp; Other business logic
              System.out.println("=="+new Date());
          }
      };
  
      public static void main(String[] args) {
  
          ScheduledExecutorService scheduler = Executors
                  .newSingleThreadScheduledExecutor();
          scheduler.scheduleWithFixedDelay(timerTask, 0, 3, TimeUnit.SECONDS);
  
      }
  }
  </code></pre>
  
  <p>For the </p>
  
  <blockquote>
    <p>Approach #3</p>
  </blockquote>
  
  <pre><code>public class Test {
  
      static Runnable task = new Runnable() {
          @Override
          public void run() {
              while (true) {
                  try {
                  // Do polling &amp; Other business logic
                      System.out.println("**"+new Date());
                      Thread.sleep(3000);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
              }
          }
      };
  
      public static void main(String[] args) {
          new Thread(task).start();
      }
  
  }
  </code></pre>
  
  <p>According to my understanding threadpools are kind of heavy objects compared to normal threads. 
  I want to know what are the pros and cons of using either of these approaches ?</p>
  
  <p>Thanks in advance.</p>
  
  <p>Regards,
  JE</p>