Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the system effect of starting large quantities of timers in Java?

recently I have had to program some applications that require large amounts of timed tasks to occur. However, I'm afraid to create so many timers because I haven't been able to figure out how they are handled by Java. Is there a problem with starting large quantities of scheduled tasks? If so, what is the better alternative?

like image 831
user113946 Avatar asked Jul 14 '09 16:07

user113946


2 Answers

If you mean one timer and a lot of tasks, the Javadocs for Timer say:

Implementation note: This class scales to large numbers of concurrently scheduled tasks (thousands should present no problem). Internally, it uses a binary heap to represent its task queue, so the cost to schedule a task is O(log n), where n is the number of concurrently scheduled tasks.

Note that there is only one thread that runs the tasks. If you need a lot of timers or more threads to run at once, you should look at java.util.concurrent.ScheduledThreadPoolExecutor.

like image 59
Kathy Van Stone Avatar answered Sep 24 '22 16:09

Kathy Van Stone


There is no problem that I can think of. But you may want to read this:

Timers

and this:

Schedule periodic tasks

like image 37
b.roth Avatar answered Sep 22 '22 16:09

b.roth