javaThreadPoolExecutor usage is briefly introduced

  • 2020-06-07 04:36:09
  • OfStack

java ThreadPoolExecutor

Preface:

If the SMS function is used in the project, 1 will generally make the SMS action asynchronous, because in most cases, whether the SMS is sent successfully or not, it will not affect the main process. Of course, operations such as sending MQ messages can also be encapsulated as asynchronous operations.

Use the basic New Thread

If you want an operation to become asynchronous, you can simply new thread and then implement the business operation in the run method. Such as:


 new Thread(new Runnable() {
    public void run() {
      // Text, text MQ News etc. 
    }
 });

But this approach has several disadvantages.

1. new1 thread will be destroyed after execution and cannot be reused;

2. If the concurrency of the system is just too large and requires a lot of threads, then new will grab resources every time.

ThreadPoolExecutor

We can use ThreadPoolExecutor in jdk1.5 to encapsulate asynchronous operations. The benefits of ThreadPoolExecutor are that they allow thread reuse and use as few threads as possible to perform more tasks, with considerable efficiency and performance. 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 is introduced


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

 }

corePoolSize: The maximum number of threads in the pool represented by corePoolSize when the element in the workQueue queue has not yet reached its maximum value;
maximumPoolSize: Maximum number of threads allowed in the thread pool;
keepAliveTime: If the number of threads in the current pool exceeds corePoolSize, those threads should be destroyed if they are idle for too long. keepAliveTime is the maximum free time for these threads;
unit: Unit of time for keepAliveTime;
workQueue: When the number of threads in the pool has reached corePoolSize, the corresponding task will be queued if the request continues.
handler: When workQueue is full and the number of threads in the pool reaches maximumPoolSize, there are no more resources to process the request and RejectedExecutionHandler is required. Reject processing, discard tasks, etc.

Implementation process

There are no threads in the thread pool when no requests are made;

When requested, create threads, 1 until the number of threads in the pool equals corePoolSize;

If there are too many requests and more threads are needed, ThreadPoolExecutor chooses to queue the request without creating a new thread.

If workQueue is full, ThreadPoolExecutor continues to create threads until the number of threads equals maximumPoolSize's.

The number of threads is up to maximumPoolSize and workQueue is slow, so you have to throw the request to RejectedExecutionHandler to process.

note

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

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: