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

android, java, multithreading, timer

Solution

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`.

Problem

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?

Original source