title: About java Timer
body: <p>I am doing some testing with Timer in java.</p>
  
  <p>My program starts a Timer, then waits for 2 seconds, cancels the Timer and prints out the j variable.</p>
  
  <p>However the Timer still runs even though I canceled it. Thanks.</p>
  
  <pre><code>public static Timer time;
  public static int j=0;
  
      public static void main(String[] args)
      {
          try {
              testrun();
              Thread.sleep(2000);
              time.cancel();
              System.out.println("timer stop");
  
              System.out.println(j);
  
          } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
      }
  
      public static void testrun()
      {
          time = new Timer();
          time.schedule(new TimerTask() {
                @Override
                public void run() {
                    for(int i = 0;i&lt;100;i++){
                    System.out.println(i);
                    j++;
                    }
  
                    System.out.println("End timer");
                }
              }, 1000, 1000);
      }
  </code></pre>