如果队列是无界的,它会调用 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/