• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to run Multiple Timer Tasks concurrently??

 
Rohan Kayan
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating multiple timerTasks and scheduling them with one timer for immediate start . Now I am confused that are they running concurrently or in sequential basic with the repetition interval of 10 seconds , what will happen if one task goes in unending loop . Will other tasks execute or not ???

Please help me out..............
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you try it, and inform us?
 
Henry Wong
author
Sheriff
Posts: 22526
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The standard java.util.Timer class only creates one thread, hence, long lived tasks will affect other tasks to be executed.

With Java 1.5, there is the ScheduledThreadPoolExecutor class, that allows you specify the number of threads.

Henry
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could make your tasks spawn new threads, too. That way they'd end almost instantly as far as the Timer thread can tell. If you have lots threads being set up and torn down, the JDK 5 thread pooling stuff might be a good idea.
 
Rohan Kayan
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:
The standard java.util.Timer class only creates one thread, hence, long lived tasks will affect other tasks to be executed.

With Java 1.5, there is the ScheduledThreadPoolExecutor class, that allows you specify the number of threads.

Henry



Thanks Henry for your reply , What will happen if I am going to use multiple Timer instance , Shall it start working concurrently or not ??
 
Henry Wong
author
Sheriff
Posts: 22526
109
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 Rohan Kayan:
Thanks Henry for your reply , What will happen if I am going to use multiple Timer instance , Shall it start working concurrently or not ??


Each Timer instance will start another thread -- so yes, you will achieve concurrency. However, if it is a repeating task, then long running tasks may still affect the next iteration of the task.

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