Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java 8 threadPoolExecutor stucks after N tasks with return statement

I am using ThreadPoolExecutor as below:

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

and :

pool.execute(()->{

//code goes here

if(some condition){
  return;
}
//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 RejectedExecutionExceptionexception

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?

like image 262
avalon Avatar asked Mar 20 '16 15:03

avalon


1 Answers

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.

Parameters:

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.

like image 86
Ravindra babu Avatar answered Nov 05 '22 07:11

Ravindra babu