• Post Reply Bookmark Topic Watch Topic
  • New Topic

Timer and TimerTask problem  RSS feed

 
galven wing
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,all:
I have a problem.I write a servlt.when the servlet does init(),
code like this:
public class T extends HttpServlet{
public void init(){
Task1 task = new Task1(); //Task1 extends TimerTask
Calendar cal=Calendar.getInstance();
Calendar newCal=newCal.set(
cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH),
0,
0,
0;
Date delay = newCal.getTime();
timer.schedule(task, delay,86400000);
}
}
public class Task1 extends TimerTask{
public void run(){
// do something
}
}
when i load the servlet, task.run() will execute right now.and some times,it will not execute right now. i dont know why this will happen.
thx
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is possible that the delay Date time is already passed.
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't say that the time is gone as the delay is computed right there. Maybe you cannot see the output of your task as you cannot say (the spec doesn't say) when the servlet is loaded.

./pope
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!