title: Java ScheduledThreadPoolExecutor running 15 milliseconds late / inconsistent time?
body: <p>I'm new too this site so forgive me if I've posted this questions in the wrong place or something :)</p>
  
  <p>I've been researching and creating test programs non-stop trying to figure out what might be going wrong with the ScheduledThreadPoolExecutor I create.</p>
  
  <p><strong>The Problem:</strong></p>
  
  <p>When running the below test application the ScheduledThreadPoolExecutor scheduled to run at a fixed rate of 3 milliseconds runs chaotically with frequent spikes of greater than 15 milliseconds. It should execute at a rate of every 3 milliseconds.</p>
  
  <p><strong>Interesting Details:</strong></p>
  
  <p>The problem <strong>only</strong> occurs outside of the IDE, the ScheduledThreadPoolExecutor runs as expected inside an IDE such as Netbeans or IntelliJ.</p>
  
  <p><strong>Question:</strong></p>
  
  <p>Why am I getting <strong>very</strong> inconsistent times using the Timer and ScheduledThreadPoolExecutor classes? The time between executions range from the expected 3 milliseconds per execution to frequent spikes of 15 milliseconds between execution.</p>
  
  <p><strong>Configuration:</strong></p>
  
  <p>Java 7 Update 5, IntelliJ IDE, Windows 7, Run from Command Prompt.</p>
  
  <p><strong>Test App Demonstrating the Problem, make sure to use outside of an IDE to see the problem. This program will output times that are greater then the expected time between execution. Should be around 15 milliseconds instead of the expected 3.</strong></p>
  
  <p>Thank you all for your help! :)</p>
  
  <pre><code>package testtime;
  
  import javax.swing.*;
  import java.awt.event.MouseAdapter;
  import java.awt.event.MouseEvent;
  import java.util.concurrent.ScheduledThreadPoolExecutor;
  import java.util.concurrent.TimeUnit;
  
  public class TestTime extends JFrame {
  
      public static void main(String args[]) {
          SwingUtilities.invokeLater(new Runnable() {
              @Override
              public void run() {
                  new TestTime().setVisible(true);
              }
          });
      }
  
      JTextArea area = new JTextArea();
      JScrollPane pane = new JScrollPane(area);
  
      ScheduledThreadPoolExecutor executor;
  
      public TestTime() {
          setSize(300, 300);
          setAlwaysOnTop(true);
          setLocationRelativeTo(null);
          setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  
          area.addMouseListener(new MouseAdapter() {
              @Override
              public void mousePressed(MouseEvent e) {
                  area.setText("--Press anywhere to start--\n\n" +
                          "If time exceeds 3 milliseconds it will be printed here...\n\n" +
                          "------------------------------------\n\n");
                  executor = new ScheduledThreadPoolExecutor(1);
                  executor.scheduleAtFixedRate(new Loop(), 0L, 3L, TimeUnit.MILLISECONDS);
              }
  
              @Override
              public void mouseReleased(MouseEvent e) {
                  executor.shutdownNow();
              }
          });
  
          area.setLineWrap(true);
          area.setWrapStyleWord(true);
          area.setText("--Press anywhere to start--");
          add(pane);
      }
  
      class Loop extends Thread {
          long time = 0L;
  
          @Override
          public void run() {
              long timeTaken = System.nanoTime() - time;
              time = System.nanoTime();
  
              if(timeTaken &gt; 3500000L) {
                  area.append(timeTaken+"(nanoseconds)  -- "+(timeTaken/1000000L)+"(milliseconds)\n");
              }
          }
      }
  }
  </code></pre>