In the actual development work, if there are too many concurrent threads, and each thread is finished after execution, such frequent creation and destruction of threads will consume time and reduce the efficiency of the system; Because thread pools are introduced.

ThreadPoolExecutor

ThreadPoolExecutor is the core class of the thread pool, with the following arguments in its constructor:

  • CorePoolSize — corePoolSize; By default, zero threads are created in the pool at initialization, threads are created when there are tasks, and the tasks are saved to the blocking queue until the number reaches corePoolSize
  • MaximunPoolSize – Indicates the maximum number of threads that can be created by the thread pool
  • KeepAliveTime – indicates how long it takes for idle threads to be destroyed; By default, this is valid only if the number of threads is greater than corePoolSize until the number does not exceed corePoolSize. If allowCoreThreadTimeOut(Boolean) is called, all threads are valid until the number is zero
  • unit
  • WorkQueue — Sets the blocking queue type
  • ThreadFactory — creates a threadFactory
  • Handler – When the number of threads reaches maximunPoolSize, all subsequent tasks are rejected. This parameter is used to set the rejection policy

Thread pool implementation principles

Thread pool state

  1. Running-0, the thread is in the RUNNING state at initialization
  2. SHUTDOWN-1: When SHUTDOWN is executed, the thread pool is not closed immediately. Instead, it does not accept new tasks and then closes the thread pool after the tasks in the queue are finished
  3. Stop-2, when shutdownNow is executed, immediately closes the thread pool, the queue does not accept the task, emptying the queue, and attempting to interrupt the task in progress
  4. Terminal-3, SHUTDOWN and STOP, the state changes to TERMINAL after the thread is destroyed and all tasks in the queue are cleared or executed

Task execution

  1. When initialized, the default number of threads in the thread pool is 0. When tasks come in, threads are created (blocking queue automatically queries tasks in the queue).
  2. When the number of threads reaches corePoolSize, incoming tasks are saved to the queue
  3. When the queue is full, continue to create new threads until the number reaches maximunPoolSize, and reject the policy
  4. When the thread is idle, it is destroyed according to keepAliveTime until the number does not exceed corePoolSize

Queue type and queue policy

  1. ArrayBlockingQueue- First in, first out, set the length of the array to be initialized
  2. LinkedBlockingQueue– First in, first out, default length integer.max_value
  3. SynchronuosQueue– Creates a new thread to execute tasks without saving them

Task rejection strategy

When the number of threads reaches maximunPoolSize and more tasks come in, the policy is rejected

  1. AbortPolicy: discards the exception thrown by the task
  2. DiscardPolicy: No exception is thrown when a task is lost
  3. DiscardOldestPolicy: Discards the first (oldest) task in the queue
  4. CallerRunsPolicy, processed by the scheduling thread

Common thread pool

  • NewFixedThreadPool, corePoolSize equals maximunPoolSize, use LinkedBlockingQueue
  • NewSingleThreadExecutor, corePoolSize and maximunPoolSize are all 1, use LinkedBlockingQueue
  • NewCacheThreadPool, corePoolSize is 0, maximunPoolSize is integer. MAX_VALUE, SynchronuosQueue is used