title: Scheduled Executor Service with single thread
body: <p>I have a sample code for Scheduled Executor Service, taken from Oracle's site. It creates a ScheduledExecutorService with core pool size o 1. It does 2 jobs: First it starts a repeated task executed at fixed intervals and then it kills the same task and the service itself after a delay.</p>
  <pre><code> ScheduledExecutorService scheduledService = Executors.newScheduledThreadPool(1);
           //This will keep executing the task at fixed interval
           ScheduledFuture&lt;?&gt; futureTask = scheduledService.scheduleAtFixedRate(new RepeatedTask(), initialDelay, interval, TimeUnit.SECONDS);
           //A future task is returned which can be used to cancel the execution after sometime
           //Here we will cancel our repeated task after 100 seconds
           scheduledService.schedule(new TaskStopper(futureTask, scheduledService), 100, TimeUnit.SECONDS);
  </code></pre>
  <p>The repeated task code:</p>
  <pre><code>public class RepeatedTask implements Runnable{
      int count = 0;
      @Override
      public void run() {
          count++;
          System.out.println(count + &quot;. Beep&quot;);
      }
  
  }
  </code></pre>
  <p>The stop task</p>
  <pre><code>@Override
      public void run() {
          mFutureTask.cancel(true);
          System.out.println(&quot;Task stopped&quot;);
          mExecutorService.shutdownNow();
          boolean shutDown = mExecutorService.isShutdown();
          if(shutDown) {
              System.out.println(&quot;Executor shutdown&quot;);
          }else {
              System.out.println(&quot;Executor not shutdown&quot;);
          }
      }
  </code></pre>
  <p>I want to understand, how does it work with a single thread in the thread pool.
  Since our executor service performs two tasks and starts them both almost at the same time,
  shouldn't we have 2 threads i.e. a ScheduledExecutorService with core pool size of 2.</p>
  <p>It works fine though. I just want to understand why it works fine with a single thread.</p>