java - 为什么ScheduledThreadPoolExecutor不提供有限队列?

标签 java concurrency

ScheduledThreadPoolExecutor 扩展了 ThreadPoolExecutor,但是 ScheduledThreadPoolExecutor 的构造函数较少,提供了无限的延迟队列,这样如果我提交太多任务,可能会导致 jvm OOM,为什么它不提供有限队列和拒绝策略(无法提交新任务时拒绝)?

最佳答案

Why does not it provide a finite queue and reject policy(reject when can't submit new task)?

您需要询问设计师,但他们决定不支持这一事实表明他们认为这没有用。 (或者至少,没有有用足以证明额外的复杂性是合理的。)

还有一个原因。 ScheduledThreadPoolExecutor 的实现取决于队列以正确的顺序返回元素。如果您查看源代码,您会发现它为此使用了自定义队列类。有一条评论是这么说的:

Using a custom queue (DelayedWorkQueue), a variant of unbounded DelayQueue. The lack of capacity constraint and the fact that corePoolSize and maximumPoolSize are effectively identical simplifies some execution mechanics (see delayedExecute) compared to ThreadPoolExecutor.

此外,很明显,延迟队列实现必须具有特定的操作属性,以便执行器正常有效地工作。这可能就是为什么他们不允许您提供自己的队列实现。

最后,如果您希望应用程序对延迟队列大小施加限制,则可以在安排新任务之前使用 yourPool.getQueue().length() 检查队列长度。

关于java - 为什么ScheduledThreadPoolExecutor不提供有限队列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55429073/

相关文章:

java - GXT 3 ToggleButtonCell 不更新数据存储

mysql - 两个查询的执行是否有可能相互干扰

java - 并行流、收集器和线程安全

java - 在类级别或方法级别泛型之间进行选择

java - 在带有时间延迟的 TextView 中显示字符 - android

java - 在 Java 中是否有将 Object 转换为 String 的标准接口(interface)?

Java 8 : Weird results combining instanceof and ternary operator

java - Collections.SynchronizedList 实现 - 同步方法与互斥锁

c# - 使用 foo[bar] = baz 添加到 ConcurrentDictionary 是线程安全的吗?

ios - 并发文件枚举