ThreadPoolExecutor
Queue Management
Capacity Blocking
Java Concurrency
Task Overflow

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.

Practice algorithms

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

  1. 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.
  2. 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.
  3. Maximum Threads Usage: If the queue is full, additional threads (beyond core) are created up to the maximum pool size.
  4. 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 RejectedExecutionException when 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 AbortPolicy is triggered, throwing a RejectedExecutionException .

Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.