user113946
user113946

Reputation: 675

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?

Upvotes: 5

Views: 815

Answers (2)

b.roth
b.roth

Reputation: 9552

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

Timers

and this:

Schedule periodic tasks

Upvotes: 2

Kathy Van Stone
Kathy Van Stone

Reputation: 26291

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.

Upvotes: 4

Related Questions