title: How to effectively cancel periodic ScheduledExecutorService task
body: <p>So, using <a href="https://stackoverflow.com/questions/4909824/stop-a-periodic-task-from-within-the-task-itself-running-in-a-scheduledexecutorse">this</a> link as a reference, can anyone suggest a more elegant solution for canceling a periodic ScheduledExecutorService task? </p>
  
  <p>Here's an example of what I'm currently doing:</p>
  
  <pre><code>// do stuff
  
  // Schedule periodic task
  currentTask = exec.scheduleAtFixedRate(
                              new RequestProgressRunnable(),
                              0, 
                              5000,
                              TimeUnit.MILLISECONDS);
  
  // Runnable
  private class RequestProgressRunnable implements Runnable
  {
          // Field members
          private Integer progressValue = 0;
  
          @Override
          public void run()
          {
              // do stuff
  
              // Check progress value
              if (progressValue == 100)
              {
                  // Cancel task
                  getFuture().cancel(true);
              }
              else
              {
                  // Increment progress value
                  progressValue += 10;
              }
          }
      }
  
      /**
       * Gets the future object of the scheduled task
       * @return Future object
       */
      public Future&lt;?&gt; getFuture()
      {
          return currentTask;
      }
  </code></pre>