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?
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
.
There is no problem that I can think of. But you may want to read this:
Timers
and this:
Schedule periodic tasks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With