• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Timer vs. ScheduledExecutorService

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
reply
    Bookmark Topic Watch Topic
  • New Topic