title: How to change a java timer's interval
body: <p>I'm working on an android Tetris game. And an IllegalStateException occurred when executing</p>
  
  <pre><code>timer.scheduleAtFixedRate (task, 0L, milliseconds);
  </code></pre>
  
  <p>in</p>
  
  <pre><code>public void setTimerInterval (int milliseconds) {
      timer.cancel ();
      timer = new Timer ();
      timer.scheduleAtFixedRate (task, 0L, milliseconds);
  }
  </code></pre>
  
  <p>Am I doing this wrongly or something?
  I need to cancel the timer and create a new one because I cannot change the interval of the timer unless you schedule a new task for it, right?</p>
  
  <p>I read a post <a href="https://stackoverflow.com/questions/7593134/how-to-modify-a-java-timer-interval-after-scheduling-it-for-a-fixed-rate-in-andr">here</a> and here is a quote of one of the answers:</p>
  
  <blockquote>
    <p>A timer can only be scheduled once. If IllegalStateException isn't happening when you call cancel(), but when you try to reschedule the timer, just reinstantiate the timer and then schedule it. Otherwise, I'm not sure.</p>
  </blockquote>
  
  <p>I didn't use the accepted answer of the that question because it's about pausing and resuming the timer.</p>
  
  <p>I reinstantiated the timer as shown above but there is still a IllegalStateException.</p>