gpt4 book ai didi

java - Java启动大量定时器对系统有什么影响?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:16:42 29 4
gpt4 key购买 nike

最近我不得不编写一些需要大量定时任务才能发生的应用程序。但是,我不敢创建这么多计时器,因为我一直无法弄清楚 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com