SingleThreadExecutor中java.util.concurrent.RejectedExecutionException的可能原因是什么 [英] What are the possible reason for a java.util.concurrent.RejectedExecutionException in a SingleThreadExecutor

查看:173
本文介绍了SingleThreadExecutor中java.util.concurrent.RejectedExecutionException的可能原因是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在单例中创建以下执行程序:

I create the following executor in a singleton:

   final private ExecutorService executor = Executors.newSingleThreadExecutor(new ThreadFactory() {
        final ThreadFactory delegate = Executors.defaultThreadFactory();
        public Thread newThread(Runnable paramAnonymousRunnable) { 
            Thread localThread =      this.delegate.newThread(paramAnonymousRunnable);
            localThread.setName("MyTask-" + localThread.getName());
            localThread.setDaemon(XXX.this.daemonThread);
            return localThread;
        }
    });

在程序执行期间,对单例的这种方法有很多调用。调用是在不同的线程中完成的,可能是在同一时间完成的。

And during the execution of the program, there a lot call to this method of the singleton. The calls are done in different threads and maybe at the sametime.

private void send(final String paramString) {
  try {
      this.executor.execute(new Runnable() {
          public void run() {
              //DO some interesting stuff
          }
      });
  } catch (Exception localException) {
    this.handler.handle(localException);
  }

}

并且在某些时候开始出现以下堆栈:

And at some point the following stacks start to appear:

java.util.concurrent.RejectedExecutionException
        at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1774)
        at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:768)
        at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:656)
        at java.util.concurrent.Executors$DelegatedExecutorService.execute(Executors.java:589)
        at XXXXX.send(XXXX.java:269)

为什么jvm会抛出这样的异常?

Why the jvm will throw such exception?

singleThreadExecutor是由LinkedBlockingQueue()支持。

并且线程池没有关闭。

The singleThreadExecutor is backed by a LinkedBlockingQueue().
And the thread pool wasn't shutdown.

有关信息,jvm是oracle jdk 1.6。单身是用弹簧创造的。来自java.util.concurrent.Executors的
副本:

for information, the jvm is oracle jdk 1.6. The singleton is created with spring. copy from java.util.concurrent.Executors:

   public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
       return new FinalizableDelegatedExecutorService
           (new ThreadPoolExecutor(1, 1,
                                0L, TimeUnit.MILLISECONDS,
                                new LinkedBlockingQueue<Runnable>(),
                                threadFactory));
   }


推荐答案

有两个原因执行会抛出 RejectedExecutionException


  1. 队列已满,您无法再添加线程

  2. ThreadPool已关闭

因为您使用的是 LinkedBlockingQueue ,所以我发现这种情况的唯一方法就是关闭池。

Since you are using a LinkedBlockingQueue the only way I can see this occurring is because you shutdown the pool.

这篇关于SingleThreadExecutor中java.util.concurrent.RejectedExecutionException的可能原因是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆