java - ScheduledThreadPoolExecutor什么时候拒绝执行?

标签 java multithreading concurrency scheduled-tasks threadpool

如果队列是无界的,它会调用 RejectedExecutionHandler 吗?

来自文档:

New tasks submitted in method execute(java.lang.Runnable) will be rejected when the Executor has been shut down, and also when the Executor uses finite bounds for both maximum threads and work queue capacity, and is saturated.

最佳答案

您发布的文档链接说明了一切。如果您指定有限边界或队列关闭,则调用 RejectedExecutionHandler。如果队列是无界的(并且我假设没有关闭),那么它永远不会调用 RejectedExecutionHandler

您可以设置一个处理程序,在出现任何问题时仅回调队列。我使用类似的东西:

// set a handler that just calls back to the queue which will block the submitter
threadPool.setRejectedExecutionHandler(new RejectedExecutionHandler() {
   public void rejectedExecution(Runnable runnable, ThreadPoolExecutor executor) {
      // this will block if the queue is full
      executor.getQueue().put(runnable);
   }
});

关于java - ScheduledThreadPoolExecutor什么时候拒绝执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17200367/

相关文章:

java - 避免使用 volatile 修复

java - 通过 Web 应用程序连接两个调用

java - 你怎么能得到JSON路径?

java - 如何在 eclipse 上使用 jena 查询本体文件

java - 中断时间超过阈值的线程的最佳实践

python - 多线程数据工作的推荐语言

java - ES删除重复项

java - 使用两个线程顺序调用两个不同函数时的通知/等待问题

Java并发: safe publication of array

Java UrlConnection 在高负载下触发 "Connection reset"异常。为什么?