2013-06-19 119 views
0

如果队列是无界的,将它曾经叫RejectedExecutionHandler?ScheduledThreadPoolExecutor何时拒绝执行?

从文档:在方法execute(一个java.lang.Runnable)提交

新任务时执行程序已关闭,将被拒绝,并且也当执行器使用有限的边界两个最大线程和工作队列容量,并已饱和。

回答

2

您发布的文档链接了一切。如果指定有限边界或队列关闭,则调用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); 
    } 
}); 
+0

所以,既然使用的ScheduledThreadPoolExecutor无限队列,假定队列没有关闭,这个自定义处理程序将永远不会被调用? – Phillip

+0

这是正确的@Phillip。 – Gray