aspose file tools*
The moose likes Threads and Synchronization and the fly likes when will the run() method be called ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "when will the run() method be called ?" Watch "when will the run() method be called ?" New topic
Author

when will the run() method be called ?

ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 375
when will the run() method of TimerTask be called ? only at the scheduled time ??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: when will the run() method be called ?