Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Executors.newCachedThreadPool throw java.util.concurrent.RejectedExecutionException during submit

Number of tasks (threads) submitted is also not huge in this test scenario.

like image 220
Krishna Kumar Avatar asked Oct 05 '09 12:10

Krishna Kumar


People also ask

What is Java Util Concurrent RejectedExecutionException?

The exception RejectedExecutionException: Task java.util.concurrent.FutureTask will be thrown for the following reasons. The thread pool is shut down before the thread is processed. The thread pool queue is complete and no further threads can be created.

What causes RejectedExecutionException?

The error message RejectedExecutionException indicates events are being raised faster than they can be handled by listeners. This typically means there are one or more plugins with @EventListener methods that are processing events directly on the event threads.

What is newCachedThreadPool Java?

static ExecutorService. newCachedThreadPool() Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. static ExecutorService. newCachedThreadPool(ThreadFactory threadFactory)

What is executors newFixedThreadPool?

The newFixedThreadPool() method of Executors class creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most n Threads will be active processing tasks.


2 Answers

You'll need to provide code samples of how you instantiate and call submit on the pool (IP should be a non-issue here as we don't need details of the internals of your Callable classes or anything like that).

Based on the information that you've given, you're almost certainly shutting down the executor service somewhere before submitting the callable to it. Check if you make any calls to shutdown or shutdownNow, and if so ensure that you're not adding tasks after this point.

Beyond that, you may want to register your own implementation of java.util.concurrent.RejectedExecutionHandler in order to aid in debugging; its rejectedExecution message will be called whenever the executor is unable to accept a task, so you could put some rudimentary state-inspection logic there to help you find the cause.

like image 90
Andrzej Doyle Avatar answered Sep 24 '22 01:09

Andrzej Doyle


I dont see anywhere in the invocation of the Executors.newCachedThreadPool() methods where a RejectedExecutionException is thrown. There are only three cases where it appears to be thrown in Java 6:

  • when calling execute() on a ThreadPoolExecutor and the maximum pool size has been reached.
  • when calling execute() on a ThreadPoolExecutor at the same time that shutdownNow, and has essentially lost the race with the shutdownNow call.
  • when trying to schedule execution of a runnable in a ScheduledThreadPoolExecutor after the executor has been shutdown.
like image 21
akf Avatar answered Sep 20 '22 01:09

akf