Hello, 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?
views:
104answers:
2There is no problem that I can think of. But you may want to read this:
http://www.javapractices.com/topic/TopicAction.do?Id=160
and this:
http://www.javapractices.com/topic/TopicAction.do;jsessionid=CADB3B0A0DB64CDE8DD4B64FB8282EF0?Id=54
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
.