title: How to stop a Runnable scheduled for repeated execution after a certain number of executions
body: <h2>Situation</h2>
  
  <p>I have a Runnable. I have a class that schedules this Runnable for execution using a ScheduledExecutorService with <a href="http://download.oracle.com/javase/1,5.0/docs/api/java/util/concurrent/ScheduledExecutorService.html#scheduleWithFixedDelay%28java.lang.Runnable,%20long,%20long,%20java.util.concurrent.TimeUnit%29" rel="noreferrer">scheduleWithFixedDelay</a>.</p>
  
  <h2>Goal</h2>
  
  <p>I want to alter this class to schedule the Runnable for fixed delay execution <em>either</em> indefinitely, <em>or</em> until it has been run a certain number of times, depending on some parameter that is passed in to the constructor.</p>
  
  <p>If possible, I would like to use the same Runnable, as it is conceptually the same thing that should be "run".</p>
  
  <h2>Possible approaches</h2>
  
  <h3>Approach #1</h3>
  
  <p>Have two Runnables, one that cancels the schedule after a number of executions (which it keeps a count of) and one that doesn't:</p>
  
  <pre><code>public class MyClass{
      private ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
  
      public enum Mode{
          INDEFINITE, FIXED_NO_OF_TIMES
      }
  
      public MyClass(Mode mode){
          if(mode == Mode.INDEFINITE){
              scheduler.scheduleWithFixedDelay(new DoSomethingTask(), 0, 100, TimeUnit.MILLISECONDS);
          }else if(mode == Mode.FIXED_NO_OF_TIMES){
              scheduler.scheduleWithFixedDelay(new DoSomethingNTimesTask(), 0, 100, TimeUnit.MILLISECONDS);
          }
      }
  
      private class DoSomethingTask implements Runnable{
          @Override
          public void run(){
              doSomething();
          }
      }
  
      private class DoSomethingNTimesTask implements Runnable{
          private int count = 0;
  
          @Override
          public void run(){
              doSomething();
              count++;
              if(count &gt; 42){
                  // Cancel the scheduling.
                  // Can you do this inside the run method, presumably using
                  // the Future returned by the schedule method? Is it a good idea?
              }
          }
      }
  
      private void doSomething(){
          // do something
      }
  }
  </code></pre>
  
  <p>I would rather just have one Runnable for the execution of the doSomething method. Tying the scheduling to the Runnable feels wrong. What do you think about this?</p>
  
  <h3>Approach #2</h3>
  
  <p>Have a single Runnable for the execution of the code that we want to run periodically. Have a separate scheduled runnable that checks how many times the first Runnable has run and cancels when it gets to a certain amount. This may not be accurate, as it would be asynchronous. It feels a bit cumbersome. What do you think about this?</p>
  
  <h3>Approach #3</h3>
  
  <p>Extend ScheduledExecutorService and add a method "scheduleWithFixedDelayNTimes". Perhaps such a class already exists? Currently, I'm using <code>Executors.newSingleThreadScheduledExecutor();</code> to get my ScheduledExecutorService instance. I would presumably have to implement similar functionality to instantiate the extended ScheduledExecutorService. This could be tricky. What do you think about this?</p>
  
  <h3>No scheduler approach [Edit]</h3>
  
  <p>I could not use a scheduler. I could instead have something like:</p>
  
  <pre><code>for(int i = 0; i &lt; numTimesToRun; i++){
      doSomething();
      Thread.sleep(delay);
  }
  </code></pre>
  
  <p>And run that in some thread. What do you think of that? You could potentially still use the runnable and call the run method directly.</p>
  
  <hr>
  
  <p>Any suggestions welcome. I'm looking for a debate to find the "best practice" way of achieving my goal.</p>