ScheduledThreadPoolExecutor何时拒绝执行?

时间:2013-06-19 20:09:57

标签: java multithreading concurrency scheduled-tasks threadpool

如果队列无限制,它是否会调用RejectedExecutionHandler?

来自文档:

  

当Executor关闭时,以及当Executor对最大线程和工作队列容量使用有限边界并且已经饱和时,将拒绝方法execute(java.lang.Runnable)中提交的新任务。

1 个答案:

答案 0 :(得分:3)

您发布的文档链接说明了一切。如果指定有限边界或队列已关闭,则调用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 r, ThreadPoolExecutor executor) {
      // this will block if the queue is full
      executor.getQueue().put(r);
   }
});