aspose file tools*
The moose likes Threads and Synchronization and the fly likes Timer vs. ScheduledExecutorService Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Timer vs. ScheduledExecutorService" Watch "Timer vs. ScheduledExecutorService" New topic
Author

Timer vs. ScheduledExecutorService

Qunfeng Wang
Ranch Hand

Joined: Jan 28, 2005
Posts: 430
Could you give me scenarios of which one to use? It seems both of them can execute tasks on schedule and in a separate thread.

Thanks.


To be or not to be. It's a question.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Louis:
It seems both of them can execute tasks on schedule and in a separate thread.


Timer uses only a single thread for the execution of all its tasks. So, if you have a task that takes a long time to process, all the subsequent tasks will be delayed.
On the other hand, you can configure the number of threads in a instance of ScheduledExecutorService. Since, the number of threads are more than one so the dependency of scheduling one task over the completion of another is reduced.

This is what the javadoc of ScheduledThreadPoolExecutor says: (this may clarify your doubt further)

This class is preferable to Timer when multiple worker threads are needed, or when the additional flexibility or capabilities of ThreadPoolExecutor (which this class extends) are required.


apigee, a better way to API!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Timer vs. ScheduledExecutorService