最近,我不得不对一些需要大量定时任务发生的应用程序进行编程.但是,恐怕会创建太多的计时器,因为我无法弄清楚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中启动大量计时器会对系统产生什么影响?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)