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

标签 java android multithreading timer

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

相关文章:

java - Gson 将 JSON 数组反序列化为对象

c# - Asp.net -- C# 中的多线程

java - Future.cancel(true) 下发生了什么

java - 字符串异常如何处理

android - 应用未在 Google Play 商店中显示

Java 安全多线程

java - CompletableFuture 与 Spring 事务

java - 一个祖先在一个 List 对象中拥有数百万个 child 是否安全?

java - 将获取的用户名放在抽屉导航 Activity 的导航 header 中

java - 这是什么意思 "No DRP key due to exception:java.lang.ClassNotFoundException: com.android.webview.chromium.Drp"?