avalon avalon - 1 year ago 86
Java Question

java 8 threadPoolExecutor stucks after N tasks with return statement

I am using

as below:

ThreadPoolExecutor pool = new ThreadPoolExecutor(cores, 50, 30L,
TimeUnit.SECONDS, new ArrayBlockingQueue<>(10));

and :


//code goes here

if(some condition){
//code goes here

And when this block with return statement is enabled, all those tasks gets stuck in TPE. TPE says that it's fully loaded and always throws

I can't understand why it happens. For example if you have a pool of size 10, and you have 100 tasks, 10 of them will match if section, you won't accept 101th task, all next tasks will be rejected. Why?

Answer Source

You did not properly configure your ThreadPoolExecutor

public ThreadPoolExecutor(int corePoolSize,
                          int maximumPoolSize,
                          long keepAliveTime,
                          TimeUnit unit,
                          BlockingQueue<Runnable> workQueue)

Creates a new ThreadPoolExecutor with the given initial parameters and default thread factory and rejected execution handler. It may be more convenient to use one of the Executors factory methods instead of this general purpose constructor.


corePoolSize - the number of threads to keep in the pool, even if they are idle, unless allowCoreThreadTimeOut is set

maximumPoolSize - the maximum number of threads to allow in the pool

keepAliveTime - when the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating.

unit - the time unit for the keepAliveTime argument

workQueue - the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method.

I have never seen TPE with workQueue size (10) less than maximumPoolSize (50). With your current configuration, 11th worker task will be rejected due to queue size of 10 ( queue size at that point of time)

Increase your workQueue size to get rid of RejectedExecutionException. 50 threads can easily handle more than 1000 small worker tasks. Configure this queue size depending on your requirement with a reasonable value.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download