English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

java ThreadPoolExecutor Usage Introduction

java ThreadPoolExecutor

Preface:

In the project, if you use the SMS sending feature, it is generally better to make the SMS sending action asynchronous because most of the time, whether the SMS is sent successfully or failed will not affect the main process. Of course, operations like sending MQ messages can also be encapsulated as asynchronous operations.

Using basic New Thread

If you want to make an operation asynchronous, you can directly new thread, and then implement the business operation in the run method. For example:

 new Thread(new Runnable() {
    public void run() {
      //Send SMS, send MQ messages, etc.
    {}
 });

However, this method has several disadvantages.

1.It will create a new thread every time, destroy it after execution, and cannot be reused;

2.If the system's concurrency is quite large and requires a large number of threads, then this way of new every time will compete for resources.

ThreadPoolExecutor

We can use jdk1.5The ThreadPoolExecutor in it wraps asynchronous operations. The advantage of ThreadPoolExecutor is that it can achieve thread reuse and use as few threads as possible to execute more tasks, which is quite efficient and performs well. The demo code is as follows:

public class ThreadPool {
  private static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(8, 12, 30,
      TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(50000), new ThreadPoolExecutor.AbortPolicy());
  public static ThreadPoolExecutor getThreadPool() {
    return threadPool;
  {}
{}

Parameter Introduction

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

corePoolSize: When the elements in the workQueue have not reached the maximum value, corePoolSize represents the maximum number of threads in the pool;
maximumPoolSize: The maximum number of threads allowed in the thread pool;
keepAliveTime: If the number of threads in the current pool exceeds corePoolSize, then the threads that exceed it should be destroyed if they are idle for too long. keepAliveTime is the maximum idle time of these threads;
unit: The time unit of keepAliveTime;
workQueue: When the number of threads in the pool has reached corePoolSize, if requests continue to come in, the corresponding task will be put into the queue;
handler: When the workQueue is full and the number of threads in the pool has reached maximumPoolSize, there are no additional resources left to handle the requests, and RejectedExecutionHandler is needed to handle them. Refuse to handle or discard tasks, etc.

Execution process

When there are no requests, there are no threads in the thread pool;

When there are requests, create threads until the number of threads in the pool equals corePoolSize;

If there are too many requests and more threads are needed to handle them, ThreadPoolExecutor chooses to put the requests into the queue and not create new threads temporarily;

If the workQueue is also full, ThreadPoolExecutor will continue to create threads until the number of threads is equal to maximumPoolSize;

When the number of threads reaches maximumPoolSize and the workQueue also slows down, the request can only be handed over to RejectedExecutionHandler for processing.

Note

When using ThreadPoolExecutor, you need to appropriately specify the size of the parameter values according to your business situation.

Thank you for reading, I hope it can help everyone. Thank you for your support of this site!