最近我不得不编写一些需要大量定时任务才能发生的应用程序。但是,我不敢创建这么多计时器,因为我一直无法弄清楚 Java 是如何处理它们的。启动大量定时任务有问题吗?如果是这样,更好的选择是什么?
最佳答案
如果你的意思是一个计时器和很多任务,Timer
的 Javadocs 说:
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.
请注意,只有一个线程运行任务。如果您需要同时运行大量计时器或更多线程,您应该查看 java.util.concurrent.ScheduledThreadPoolExecutor
。
关于java - Java启动大量定时器对系统有什么影响?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1126555/