Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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) 

Why the jvm will throw such exception?

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

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));    } 
like image 290
RJO Avatar asked Sep 25 '13 11:09

RJO


People also ask

What is Java Util Concurrent RejectedExecutionException?

RejectedExecutionException: rejected from java. util. concurrent. ThreadPoolExecutor error occurs when the thread pool queue is full and no further threads can be created in spring boot.

What causes RejectedExecutionException?

The RejectedExecutionException exception is thrown when a task can't be accepted by the executor for execution. Generally there are two scenarios when a task is rejected for execution: The executor service has already been shut down.

What is Java util concurrent executors?

The concurrent API in Java provides a feature known as an executor that initiates and controls the execution of threads. As such, an executor offers an alternative to managing threads using the thread class. At the core of an executor is the Executor interface.

How do you handle RejectedExecutionException?

If you have constrained your thread pool to only allow a certain number of concurrent threads (generally a good thing), then the application needs to somehow push-back on the calling code, so when you receive a RejectedExecutionException from the ThreadPoolExecutor you need to indicate this to the caller and the caller ...


2 Answers

There are two reasons why execute would throw a RejectedExecutionException

  1. The queue is full and you cannot add any more threads
  2. The ThreadPool has been shutdown

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

like image 160
John Vint Avatar answered Sep 21 '22 05:09

John Vint


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

    executor.shutdown();     executor.awaitTermination(10, TimeUnit.MINUTES); 
like image 33
Andrey Chaschev Avatar answered Sep 19 '22 05:09

Andrey Chaschev