Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ThreadPoolExecutor with unbounded queue not creating new threads

Tags:

My ThreadPoolExecutor is failing to create new threads. In fact I wrote a somewhat hacky LinkedBlockingQueue that will accept any task (i.e. it is unbounded) but call an additional handler - which in my application spews warning trace that the pool is behind - which gives me very explicit information that the TPE is refusing to create new threads even though the queue has thousands of entries in it. My constructor is as follows:

private final ExecutorService s3UploadPool =  new ThreadPoolExecutor(1, 40, 1, TimeUnit.HOURS, unboundedLoggingQueue); 

Why is it not creating new threads?

like image 598
djechlin Avatar asked Mar 18 '13 20:03

djechlin


1 Answers

This gotcha is covered in this blog post:

This construction of thread pool will simply not work as expected. This is due to the logic within the ThreadPoolExecutor where new threads are added if there is a failure to offer a task to the queue. In our case, we use an unbounded LinkedBlockingQueue, where we can always offer a task to the queue. It effectively means that we will never grow above the core pool size and up to the maximum pool size.

If you also need to decouple the minimum from maximum pool sizes, you will have to do some extended coding. I am not aware of a solution that exists in the Java libraries or Apache Commons. The solution is to create a coupled BlockingQueue that is aware of the TPE, and will go out of its way to reject a task if it knows the TPE has no threads available, then manually requeue. It is covered in more detail in linked post. Ultimately your construction will look like:

public static ExecutorService newScalingThreadPool(int min, int max, long keepAliveTime) {    ScalingQueue queue = new ScalingQueue();    ThreadPoolExecutor executor =       new ScalingThreadPoolExecutor(min, max, keepAliveTime, TimeUnit.MILLISECONDS, queue);    executor.setRejectedExecutionHandler(new ForceQueuePolicy());    queue.setThreadPoolExecutor(executor);    return executor; } 

However more simply set corePoolSize to maxPoolSize and don't worry about this nonsense.

like image 78
djechlin Avatar answered Oct 02 '22 16:10

djechlin