This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

when will the run() method be called ?

 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when will the run() method of TimerTask be called ? only at the scheduled time ??
 
Henry Wong
author
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ideally, the run() method should only be called at the scheduled time... However...

- The Timer class only uses one thread, so a TimerTask may be called late if another task is currently running. With enough repeating timer tasks, a backlog could even develop.

- The Timer class uses the system time to calculate the wait times, so you can get weird results if you change the system time.

Henry
[ June 30, 2006: Message edited by: Henry Wong ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The doc says "Corresponding to each Timer object is a single background thread..." so it sounds like you get a new thread each time you do new Timer(). That would be interesting to confirm.
 
Henry Wong
author
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
The doc says "Corresponding to each Timer object is a single background thread..." so it sounds like you get a new thread each time you do new Timer(). That would be interesting to confirm.


Yes, each instance of the Timer class should have its own thread. So if you have a task that is long running, (or is schedulling critical), you could consider giving it to its own timer.

Henry
[ July 01, 2006: Message edited by: Henry Wong ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic