preface

In the peacetime development, often encountered such things, such as database connection pool, Web request is also the use of pooling technology, but we are less direct contact;

The body of the

What is a thread pool

A simple point of understanding is a pool of threads, a pooling idea for a certain number of threads management, such as: thread creation, destruction, task execution, etc.;

Thread pool flow

  1. Submit tasks to the thread pool
  2. Check whether the number of core threads in the thread pool is full. If not, create a core thread to handle the task. Otherwise, proceed to the next process
  3. Check whether the number of queues in the thread pool is full. If not, add the task to the waiting queue; otherwise, proceed to the next flow
  4. Check whether the maximum number of threads in the thread pool is full. If not, create non-core threads to process tasks. Otherwise, proceed to the next process
  5. The task is processed according to the reject policy configured by the thread pool

The flow chart is as follows:

The thread pool

ThreadPoolExecutor

Create a thread pool from ThreadPoolExecutor in Java with the following parameters:

public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler) {}
Copy the code

Parameter Description:

  1. corePoolSize

    • The number of threads that remain in the thread pool, even if they are idle, unless setallowCoreThreadTimeOut
    • Threads in the thread pool will not be created until the task is submitted, if the thread pool’sprestartAllCoreThreads()The thread pool creates the core threads ahead of time and starts them up
  2. maximumPoolSize

    • The maximum number of threads allowed to be created by the thread pool. When the queue configured by the thread pool is full, non-core threads will continue to be created until the maximum number of threads is created
  3. keepAliveTime

    • The lifetime of idle (non-core) threads when the number of threads is greater than the number of core threads, after which idle threads are reclaimed
  4. unit

    • keepAliveTimeUnit of time
  5. workQueue

    • A work queue that saves tasks before they are executed. This queue stores only the number of tasksexecuteMethod submittedRunnabletask
  6. threadFactory

    • Thread creation factory, which is used when a new thread is created
    • This is where you typically set the name of the thread (implementationThreadFactoryOf the interfacenewThreadMethods)
  7. handler

    • There are four main types of saturation strategies
    • This policy is used when the number of work queues and threads reaches the maximum

Four rejection strategies:

  1. AbortPolicy: Throws an exception. Default option
  2. DiscardPolicy: Discards tasks directly
  3. DiscardOldestPolicy: Discards the oldest task in the queue and saves the new one
  4. CallerRunsPolicy: Returned to the calling thread to handle the task

Custom rejection policy: Define a class that implements the rejectedExecution method of the RejectedExecutionHandler interface

Four types of blocking queues:

  1. ArrayBlockingQueue: Array-based bounded blocking queue, initialized to a specified capacity, first-in, first-out
  2. LinkedBlockingQueue: An unbounded block queue based on a linked list structure, with which the pool of threads has not reached its maximum number
  3. SynchronousQueue: synchronousblocking queue; Each insertion queue must wait for another thread to remove it, and vice versa;
  4. PriorityBlockingQueue: Priority queue
  5. DelayedWorkQueue: indicates the delay queueScheduledThreadPoolExecutorStatic class
Running status of ThreadPoolExecutor

ThreadPoolExecutor can run in the following states:

  1. RUNNING: Receives new tasks and processes tasks in the queue
  2. SHUTDOWN: does not accept new tasks, but processes tasks in the queue
  3. STOP: does not accept new tasks, does not process tasks in the queue, and interrupts tasks in progress
  4. TIDYING: All tasks end, thread count is zero, thread pool status changesTIDYINGWill runterminatedmethods
  5. TERMINATED:terminatedMethod completed

The flow chart is as follows:

Several ways to create threads

The following ways to create thread pools all use ThreadPoolExecutor to create thread pools;

  1. Executors.newCachedThreadPool();

The source code is as follows:

public static ExecutorService newCachedThreadPool(a) {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
    }
Copy the code
  • The core thread is 0

  • The maximum number of threads is integer.max_value

  • The idle thread lifetime is 60 seconds

  • Use SynchronousQueue

  1. Executors.newFixedThreadPool(15);
 public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }
Copy the code
  • Core threads are equal to the maximum number of threads
  • The idle thread lifetime is 0
  • Use unbounded blocking queues
  1. Executors.newScheduledThreadPool(1);

Scheduled task thread pool

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
        return new ScheduledThreadPoolExecutor(corePoolSize);
    }

public ScheduledThreadPoolExecutor(int corePoolSize) {
        super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
              new DelayedWorkQueue());
    }
Copy the code
  • The number of core threads is corePoolSize and the maximum number of threads is integer.max_value
  • The idle thread lifetime is 0
  • Use the internal delay queue DelayedWorkQueue
  1. Executors.newSingleThreadExecutor();

There is only one core thread, and the largest thread is the core thread

public static ExecutorService newSingleThreadExecutor(a) {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1.1.0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
    }
Copy the code
  • The number of core threads is the same as the maximum number of threads
  • The idle thread lifetime is 0
  • Use the unbounded blocking queue LinkedBlockingQueue

Thread pool monitoring

Thread pool built-in methods:

The methods in the figure above give you a look inside the thread pool at runtime; In addition, ThreadPoolExecutor has three methods that can be customized:

  1. Void beforeExecute(Thread t, Runnable r) This method is executed before a task is executed

  2. Void afterExecute(Runnable r, Throwable t) afterExecute(Runnable r, Throwable t) afterExecute(Runnable r, Throwable t) afterExecute(Runnable r, Throwable t)

  3. The void terminated() thread pool is terminated by calling the shutdown(),shutdownNow(),remove methods, and so on.

The last

This article has only covered some basic information about thread pools;

reference

  1. Thread pools in Java
  2. Java thread pool analysis
  3. Java thread pool details
  4. Understanding Java thread pools in depth: ThreadPoolExecutor
  5. Java High Concurrency series – Day 18: Java Thread Pools, this article is enough
  6. Implementation principle of Java thread pool and its practice in Meituan business
  7. Java thread pool parsing