title: Java util.Timer, how to reset?
body: <p>I am trying to build more or less a watchdog class. If a value hasn't changed for a certain time, the class should do something.
  Therefore I want to utilize a standard timer, the idea is to set a timer, and reset it as soon as the watched value changes. </p>
  
  <pre><code>public class Watchdog {
      public final Timer TIMER = new Timer(true);
      public final long DELAY;
      public Watchdog(long delay){
          DELAY=delay;
          valueChanged();
      }
  
      public void valueChanged(){
          TIMER.cancel();
          TIMER.purge();
          TIMER.schedule(new TimerTask() {
  
              @Override
              public void run() {
                  Watchdog.this.alarm();
              }
          }, DELAY);
      }
  
      public void alarm(){
          System.out.println("Watchdog barked");
      }
  }
  </code></pre>
  
  <p>Unfortunately I get an IllegalStateException every time I call <code>TIMER.cancel()</code>, as there is no timer set at the first call. What am I doing wrong? How to reset the timer, even if there is no timer set?</p>
  
  <p><strong>EDIT</strong>
  Stacktrace</p>
  
  <pre><code>Exception in thread "main" java.lang.IllegalStateException: Timer already cancelled.
  at java.util.Timer.sched(Timer.java:397)
  at java.util.Timer.schedule(Timer.java:193)
  at deleteme.Watchdog.valueChanged(Watchdog.java:28)
  at deleteme.Watchdog.&lt;init&gt;(Watchdog.java:22)
  at deleteme.Deleteme.main(Deleteme.java:19)
  Java Result: 1
  </code></pre>