SingleThreadExecutor 中 java.util.concurrent.RejectedExecutionException 的可能原因是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19003430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:17:12  来源:igfitidea点击:

What are the possible reason for a java.util.concurrent.RejectedExecutionException in a SingleThreadExecutor

javamultithreading

提问by RJO

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.

并且在程序执行过程中,会大量调用singleton的这个方法。调用是在不同的线程中完成的,也可能是同时完成的。

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)

Why the jvm will throw such exception?

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

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

singleThreadExecutor 由 LinkedBlockingQueue() 支持。
并且线程池没有关闭。

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

有关信息,jvm 是 oracle jdk 1.6。单例是用 spring 创建的。从 java.util.concurrent.Executors 复制:

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

回答by cgon

Maybe you should use a thread pool instead of using a single executor.

也许您应该使用线程池而不是使用单个执行程序。

    executor = new java.util.concurrent.ThreadPoolExecutor(30, 30, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new ThreadFactory() {
        final AtomicInteger threadNumber = new AtomicInteger( 1 );
        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r, "Thread No : " + threadNumber.getAndIncrement());
        }
    });

回答by Andrey Chaschev

You might have submitted tasks after calling executor.shutdown(). Normally to stop executor they do

您可能在调用 之后提交了任务executor.shutdown()。通常停止执行者他们做

    executor.shutdown();
    executor.awaitTermination(10, TimeUnit.MINUTES);

回答by John Vint

There are two reasons why executewould throw a RejectedExecutionException

有两个原因execute会抛出一个RejectedExecutionException

  1. The queue is full and you cannot add any more threads
  2. The ThreadPool has been shutdown
  1. 队列已满,您无法添加更多线程
  2. 线程池已关闭

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

由于您使用的LinkedBlockingQueue是我能看到这种情况发生的唯一方法是因为您关闭了池。

回答by Vishnu Vardhana Reddy

Threads are not available to execute the given task. No linked block queue to que task.

线程不可用于执行给定的任务。没有链接到队列任务的块队列。