Advantages of thread pools

  1. Reduce system resource consumption by reusing existing threads, reducing the overhead caused by thread creation and destruction;
  2. Improve system response speed. When a task arrives, it can be executed immediately without waiting for a new thread to be created by reusing the existing thread.
  3. It is convenient to control the number of concurrent threads, because threads are scarce resources. If they are created without limit, they may occupy too much memory and generate OOM, and cause excessive CPU switching. Thread pools enable uniform allocation, tuning, and monitoring.
  4. Provides more powerful function for delayed timing thread pool.

The main parameters of the thread pool

public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
    this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
         Executors.defaultThreadFactory(), defaultHandler);
}
Copy the code

1 corePoolSize: indicates the number of core threads

When the number of threads is smaller than the number of core threads, even if there are idle threads, the thread pool will preferentially create new threads handling allowCoreThreadTimeout=true (default false), the core thread will timeout and shut down

2 queueCapacity: task queueCapacity (blocking queue)

When the number of core threads reaches its maximum, new tasks are queued for execution

3 maxPoolSize: specifies the maximum number of threads

When the number of threads >=corePoolSize and the task queue is full. If the number of threads is equal to maxPoolSize and the task queue is full, the thread pool will refuse to process the task and throw an exception

4 keepAliveTime: indicates the idle time of a thread

When the thread idle time reaches keepAliveTime, the thread exits until the number of threads =corePoolSize and if allowCoreThreadTimeout=true, until the number of threads =0

5 allowCoreThreadTimeout: Allows the core thread to timeout

6 threadFactory indicates the threadFactory

Factory used when creating a new thread, which can be used to specify the thread name, whether it is a daemon thread, etc

7 rejectedExecutionHandler: task rejection handler

When the number of threads has reached maxPoolSize and the cut queue is full, new tasks are rejected. When shutdown() is called on the thread pool, it waits for all tasks in the thread pool to complete and then shuts down. If a task is submitted between the call to shutdown() and the actual shutdown of the thread pool, the new task is rejected and the thread pool calls rejectedExecutionHandler to handle the task. If AbortPolicy is not set by default, an exception is thrown

The ThreadPoolExecutor class has several internal implementation classes to handle such cases: AbortPolicy Discards the task, discard runtime exceptions CallerRunsPolicy Executing the task DiscardPolicy Ignore, Implementing the RejectedExecutionHandler interface With DiscardOldestPolicy DiscardOldestPolicy removes the first entered task from the queue

Thread pool flow