Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Timer Thread is not closed

 
Lalit mishra
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.Timer;
import java.util.TimerTask;
public class TimerCheck
{
Timer t = null;
TimerCheck tc;
public void main() {
tc = new TimerCheck();
tc.t = new Timer();
tc.t.schedule(new TimerCheck().new demo(), 5000);
System.out.println("In main check");
//tc.t.cancel();
}
public void hello(){

System.out.println("its working");
//tc.t.cancel();
}
public static void main(String args[]){
new TimerCheck().main();
}
class demo extends TimerTask{
public void run(){
hello();
}
}

}

Hi the above program works fine but the Timer thread is not stopped.
how can i call cancel() method to close the thread.....
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi the above program works fine but the Timer thread is not stopped.

Why do you think so ?
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first thing that pops up in my mind is: why are you creating TimerCheck objects in 3 different places? Shouldn't a single one be sufficient?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic