title: Why is java ExecutorService newSingleThreadExecutor spawning two threads?
body: <p>I have a sample java code below which if run as a console application behaves as I expected ( spawning a single thread to execute the runnable). </p>
  
  <p>The strange behavior (spawning two threads - sample below) I see is when I run this sample as a service application using Apache's prunsrv64.exe.</p>
  
  <p>I am testing this on a Windows 7 machine - 64bit.</p>
  
  <p>Sample Output:</p>
  
  <pre><code>   Thread -28 Current time: 09:50:11 AM
     Thread -52 Current time: 09:50:12 AM
     Thread -28 Current time: 09:50:21 AM
     Thread -52 Current time: 09:50:22 AM
     Thread -28 Current time: 09:50:31 AM
     Thread -52 Current time: 09:50:32 AM
  </code></pre>
  
  <p>Sample Code:</p>
  
  <pre><code>import java.util.Date;
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
  
  public class ExecutorTest{
      public void testIt(){
          ExecutorService ex = Executors.newSingleThreadExecutor();
          ex.execute(new Runnable(){
              public void run() {
                  while(true){
                      System.out.printf("Thread -" + Thread.currentThread().getId() + " Current time: %tr%n", new Date());
                      try{
                          Thread.sleep(10000);    
                      }catch(InterruptedException ie){
                          ie.printStackTrace();
                      }                   
                  }
  
              }
          });     
      }
  }
  </code></pre>
  
  <p>Thanks.</p>
  
  <p>Update:
  Just to clarify I am calling this code as follows:</p>
  
  <pre><code>    ExecutorTest tester = new ExecutorTest();
      tester.testIt();
  </code></pre>
  
  <p>The same code without changes is behaving differently when run as console application and a service application as I mentioned above.</p>
  
  <hr>
  
  <p>Update 2:
  I added a second tester which uses a ScheduledExecutorService. The behavior is the same.</p>
  
  <p>Update2 Output:</p>
  
  <pre><code>Using ScheduledExecutorService.
  Thread Id outside Runnable -1
  Thread -53 Current time: 10:58:15 AM
  Thread -28 Current time: 10:58:24 AM
  Thread -53 Current time: 10:58:25 AM
  Thread -28 Current time: 10:58:34 AM
  Thread -53 Current time: 10:58:35 AM
  Thread -28 Current time: 10:58:44 AM
  Thread -53 Current time: 10:58:45 AM
  Thread -28 Current time: 10:58:54 AM
  Thread -53 Current time: 10:58:55 AM
  Thread -28 Current time: 10:59:04 AM
  Thread -53 Current time: 10:59:05 AM
  </code></pre>
  
  <p>Update 2 Code:</p>
  
  <pre><code>public void testItWithScheduled(){
      System.out.println("Using ScheduledExecutorService.");
      ScheduledExecutorService ex = Executors.newSingleThreadScheduledExecutor();
      System.out.println("Thread Id outside Runnable -" + Thread.currentThread().getId());
      ex.scheduleWithFixedDelay(new Runnable(){
          public void run() {
              System.out.printf("Thread -" + Thread.currentThread().getId() + " Current time: %tr%n", new Date());
          }
      },0L, 10, TimeUnit.SECONDS);        
  }
  
  
  called through:
  
      ExecutorTest tester = new ExecutorTest();
      tester.testItWithScheduled();
  </code></pre>
  
  <hr>
  
  <p>Update 3:
  Modified logging to add identity hash</p>
  
  <pre><code>Using ScheduledExecutorService.
  Thread Id outside Runnable 1 with reference: 1370756928
  Thread -53 Current time: 11:10:38 AM with reference: 1370756928
  Thread -28 Current time: 11:10:47 AM with reference: 1939972532
  Thread -53 Current time: 11:10:48 AM with reference: 1370756928
  Thread -28 Current time: 11:10:57 AM with reference: 1939972532
  Thread -53 Current time: 11:10:58 AM with reference: 1370756928
  Thread -28 Current time: 11:11:07 AM with reference: 1939972532
  Thread -53 Current time: 11:11:08 AM with reference: 1370756928
  </code></pre>