title: How to cancel an already scheduled TimerTask?
body: <p>I have a tiny problem that I can't seem to do right. I have the following class in java:</p>
  
  <pre><code>package pooledtcpconnector.utilities;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.util.Timer;
  import java.util.TimerTask;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  
  public final class Notifier implements Runnable {
  
      private final ILogger logger;
  
      private Timer mTimer;
      private final int Treshold;
      private final InputStream ResponseStream;
      private final TimerTask DoWaitTask;
  
      public Notifier(final InputStream _ResponseStream, final Integer _Treshold, final ILogger logger) {
          this.logger = logger;
  
          mTimer = new Timer();
  
          this.ResponseStream = _ResponseStream;
          this.Treshold = _Treshold;
  
          DoWaitTask = new TimerTask() {
              @Override
              public void run() {
                  try {
                      int mSize = ResponseStream.available();
                      if (mSize &gt;= Treshold) {
                          mTimer.cancel();
                      }
                  } catch (final IOException ex) {
                      final String ExceptionMessage = ex.getMessage();
                      logger.LogMessage(
                              this.getClass().getCanonicalName(),
                              "Notifier.DoWaitTask:run.ResponseStream.available",
                              ex.getClass().getCanonicalName(),
                              new String[]{
                                  ExceptionMessage,
                                  "Parameters:",
                                  String.format("-"),
                              });
  
                      Logger.getLogger(Notifier.class.getCanonicalName()).log(Level.FINE, ex.getMessage(), ex.getCause());
                  }
              }
          };
      }
  
      @Override
      public void run() {
          synchronized (this) {
              mTimer.scheduleAtFixedRate(DoWaitTask, 250, 200);
              // Notification mechanism
              notify();
          }
      }
  
  }
  </code></pre>
  
  <p>This class would ensure that our application won't start processing the SocketInputStream unless the available method returns at least Treshold.  The problem however is that, once I schedule the DoWaitTask with the Timer it runs for eternity. By cancelling the timer the task still runs and the whole application hangs, but more importantly it tries to call available on the stream once it already has been processed and closed. Of course this results in a nice IOException: stream closed.</p>
  
  <p>How could I stop the scheduled task along with the timer? timer.cancel obviously isn't enough.</p>
  
  <p>Regards,
   Joey</p>