What could be the reason that the thread will stand on java.util.concurrent.ThreadPoolExecutor.getTask

I have an executor who is responsible for consuming messages from ArrayBlockingQueue.

new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(1), r -> { return new Thread(r, "Request-Queue-Drainer"); }); 

The request-queue-drainage flow is in the WAITING state (although tasks are transferred to this thread). The following is a stream dump.

 Name: Request-Queue-Drainer State: WAITING on java.u til.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject@ 5b4757a2 Total blocked: 0 Total waited: 8 Stack trace: sun.misc.Unsafe.park(Native Method) java.util.concurrent.locks.LockSupport.park(LockSupport.java:175) java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039) java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442) java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) java.lang.Thread.run(Thread.java:745) 

What could be the reason that the thread is idle?

+5
source share
1 answer

The thread is waiting because the queue is empty. What the pool threads do: they select tasks from the queue and start them, and when the queue is empty, they wait.


Edit: there is something else that can happen when the queue is empty: ThreadPoolExecutor may send a poison pill message that causes the pool thread to die if the number of workers is greater than corePoolSize for more than keepAlive time units. This will not happen in your case, because you set maximumPoolSize and corePoolSize to the same number (1).


I do not expect your ThreadPoolExecutor be different from that returned by Executors.newFixedThreadPool(1) , except that yours can only have one incomplete task, and it gives the pool thread a special name.

+4
source

All Articles