ThreadPoolExecutor Block When its Queue Is Full?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
When working with concurrent applications in Java, especially when dealing with thread management, the ThreadPoolExecutor
class is a powerful utility. It provides a pool of worker threads, allowing the execution of tasks concurrently and managing thread life cycles. However, understanding how it behaves when its queue is full is crucial for designing robust and efficient systems. Let's delve into the details.
Understanding ThreadPoolExecutor
ThreadPoolExecutor
is part of Java's concurrency framework, located in the java.util.concurrent
package. It is designed to manage a pool of threads for executing submitted tasks, offering configuration options for core pool size, maximum pool size, keep-alive time, and more.
Key Components:
- Core Pool Size: The number of threads to keep in the pool, even if they are idle.
- Maximum Pool Size: The maximum number of threads allowed in the pool.
- Keep-Alive Time: The time that excess idle threads will wait for new tasks before terminating.
- Task Queue: A blocking queue to hold tasks before they are executed. This acts as a buffer.
How Tasks Are Managed
- Core Threads Handling: Initially, a new task is executed using a core thread. If a core thread is available, it is used to execute the task immediately.
- Queueing Tasks: If all core threads are busy, the task is placed into a queue. The queue type can affect the executor's behavior:
- SynchronousQueue: Directly hands off tasks to threads. Results in immediate task rejection if no threads are available.
- LinkedBlockingQueue: Supports unbounded queues, hence handles tasks without congestion by queuing them while core threads are busy.
- ArrayBlockingQueue: A bounded queue with specified capacity. Tasks are queued up to its limit.
- Maximum Threads Usage: If the queue is full, additional threads (beyond core) are created up to the maximum pool size.
- Task Rejection: If both the queue is full and the maximum pool size has been reached, the task is rejected.
Task Rejection and RejectedExecutionHandler
When the task queue overflows, ThreadPoolExecutor
provides the mechanism to handle such cases using the RejectedExecutionHandler
. This serves as a strategy to manage tasks that cannot be executed by default due to capacity constraints. Java provides several policies:
- AbortPolicy: Throws
RejectedExecutionExceptionwhen a task cannot be submitted. - CallerRunsPolicy: The task is executed in the caller's thread, effectively blocking the task submission until some thread becomes available.
- DiscardPolicy: Silently discards the task.
- DiscardOldestPolicy: Discards the oldest unhandled task and attempts to execute the current task.
Example Scenario: Queue Full in ThreadPoolExecutor
Consider a scenario where a ThreadPoolExecutor
is used with a core pool size of 2, maximum pool size of 4, and an ArrayBlockingQueue
with a capacity of 2. The executor is defined as follows:
- Two tasks are executed immediately using the core threads.
- Two tasks are placed in the queue.
- The next task submission leads to a new thread creation beyond core size up to the maximum size.
- When attempting to submit the 5th and 6th tasks, since both the queue and maximum pool size are reached, the
AbortPolicyis triggered, throwing aRejectedExecutionException.
Related reading
- Tickmark algorithm for a graph axis
- Tie breaking in a priority queue using python
- Time complexity deleting element of deque
- Time complexity for Dijkstra's algorithm with min heap and optimizations
- ThreadPoolExecutor with corePoolSize 0 should not execute tasks until task queue is full
- ThreadPool.QueueUserWorkItem vs Task.Factory.StartNew
- Thread.sleep VS Executor.scheduleWithFixedDelay
- throw checked Exceptions from mocks with Mockito

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.