title: How to cancel scheduled Timer Task and run it again?
body: <p>First of all this is a Minecraft Forge mod but i am sure that you don't need to know forge to solve my issue.</p>
  <p>I simply use Keybind &quot;K&quot; to schedule 3 different timers and after that i click &quot;L&quot; to cancel them running again. There is no problem until i start &quot;K&quot; (again) to start my timers again but i get</p>
  <p>&quot;java.lang.IllegalStateException: Timer already cancelled.&quot; error in logs.</p>
  <p>The code that i use to manage timers:</p>
  <pre><code>public class KeyBind {
  public static Boolean Status = false;
  Timer timer = new Timer();
  
  
  
  @SubscribeEvent
  public void onKeyPressed(InputEvent.KeyInputEvent event) throws InterruptedException {
      if (ForgeMain.keybinding.isKeyDown()) {
          if(Status == false) {
              EntityPlayerSP entityPlayerSP = (Minecraft.getMinecraft()).thePlayer;
              entityPlayerSP.addChatComponentMessage((IChatComponent) new ChatComponentText(EnumChatFormatting.YELLOW + &quot;You have successfully ran the NetherWart Macro!&quot;));
              Status = true;
              timer.schedule(new Left(), 100, 38000);
              timer.schedule(new Forward(), 33100, 38000);
              timer.schedule(new Right(), 35100, 38000);
          }
      }if(ForgeMain.keybinding1.isKeyDown()){
          if(Status == true) {
              Status = false;
              System.out.println(Status.booleanValue());
              timer.cancel();
              timer.purge();
          }
  
          }
      }
  </code></pre>
  <p>And here is one of my timers (They all work the same way differences are the direction to move and  moving time):</p>
  <pre><code>public class Right extends TimerTask {
  @Override
  public void run() {
      int left = Minecraft.getMinecraft().gameSettings.keyBindRight.getKeyCode();
  
      long oldTime = System.currentTimeMillis();
  
      while (System.currentTimeMillis() - oldTime &lt; 33000) {
          KeyBinding.setKeyBindState(left, true);
      }
      KeyBinding.setKeyBindState(left, false);
  
  }
  </code></pre>
  <p>}</p>
  <p>Edit:</p>
  <p>Okey i learnt that i have to create another timer now i use:</p>
  <pre><code>    @SubscribeEvent
  public void onKeyPressed(InputEvent.KeyInputEvent event) throws InterruptedException {
      if (ForgeMain.keybinding.isKeyDown()) {
          if(Status == false) {
              EntityPlayerSP entityPlayerSP = (Minecraft.getMinecraft()).thePlayer;
              entityPlayerSP.addChatComponentMessage((IChatComponent) new ChatComponentText(EnumChatFormatting.YELLOW + &quot;You have successfully ran the NetherWart Macro!&quot;));
              Status = true;
              Timer timer = new Timer();
              timer.schedule(new Left(), 100, 38000);
              timer.schedule(new Forward(), 33100, 38000);
              timer.schedule(new Right(), 35100, 38000);
          }
      }if(ForgeMain.keybinding1.isKeyDown()){
          if(Status == true) {    
              Status = false;
              System.out.println(Status.booleanValue());
              timer.cancel();
              timer.purge();
          }
  
          }
      }
  </code></pre>
  <p>}</p>
  <p>But now i do not know how to cancel the timer (in code timer.cancel(); ofc does not work.)</p>