java - RejectedExecutionException 的原因可能是什么

标签 java multithreading

我在我的 tomcat 服务器 (+liferay) 上遇到此异常

java.util.concurrent.RejectedExecutionException

我的课是这样的:

public class SingleExecutor extends ThreadPoolExecutor {
  public SingleExecutor(){
    super(1, 1,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());
  }

  @Override
  public void execute(Runnable command) {
    if(command instanceof AccessLogInsert){
        AccessLogInsert ali = (AccessLogInsert)command;
        ali.setConn(conn);
        ali.setPs(ps);
    }
    super.execute(command);
  }
}

我在 super.execute(command); 行得到了这个异常 当队列已满但 LinkedBlockingQueue 大小为 2^31 时,可能会出现此错误,并且我确信没有那么多命令等待。

一开始一切都很稳定,但在我重新部署 war 后它开始发生。这个类不是 war 的一部分,而是在 tomcat/lib 的一个 jar 中。

您知道为什么会发生这种情况以及如何解决吗?

最佳答案

来自 ThreadPoolExecutor JavaDoc(强调我的)

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. In either case, the execute method invokes the RejectedExecutionHandler.rejectedExecution(java.lang.Runnable, java.util.concurrent.ThreadPoolExecutor) method of its RejectedExecutionHandler. Four predefined handler policies are provided:

  1. In the default ThreadPoolExecutor.AbortPolicy, the handler throws a runtime RejectedExecutionException upon rejection.
  2. In ThreadPoolExecutor.CallerRunsPolicy, the thread that invokes execute itself runs the task. This provides a simple feedback control mechanism that will slow down the rate that new tasks are submitted.
  3. In ThreadPoolExecutor.DiscardPolicy, a task that cannot be executed is simply dropped.
  4. In ThreadPoolExecutor.DiscardOldestPolicy, if the executor is not shut down, the task at the head of the work queue is dropped, and then execution is retried (which can fail again, causing this to be repeated.)

It is possible to define and use other kinds of RejectedExecutionHandler classes. Doing so requires some care especially when policies are designed to work only under particular capacity or queuing policies.

因此,重新加载 war 可能会触发 Executor 的关闭。尝试将相关库放入 war 中,以便 Tomcat 的 ClassLoader 有更好的机会正确重新加载您的应用程序。

关于java - RejectedExecutionException 的原因可能是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8183205/

相关文章:

java - ServiceTracker、服务引用和服务引用之间的区别

java - SessionMap 不保存 JSF 对象

python - 在 python 中运行时将数据从 GUI 传递到线程

ruby - ruby 异常如何导致 mutices 解锁?

java - JVM字符串池线程是本地的吗?它会导致此用例出现任何问题吗?

java - 在 jsp foreach 中保留 Controller 的索引

java - micronaut petstore 从java到groovy的代码段

java - 单元测试私有(private)字段

java - 在 AsyncTask 中同步两个并发网络调用

c - Pthreads - 高内存使用率