Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Introduction to thread pool class ThreadPoolExecutor

Shulou Source: shulou.com Published: 2022-06-03 02:33:09 10月02日 Update

Threadpool class is java.util.concurrent. ThreadPoolExecutive, and common construction methods are:

ThreadPoolExecutor(intcorePoolSize, int maximumPoolSize,

longkeepAliveTime, TimeUnit unit, BlockingQueueworkQueue,

RejectedExecutionHandlerhandler)

corePoolSize: Minimum number of threads maintained by the thread pool

maximumPoolSize: Maximum number of threads maintained by the thread pool

keepAliveTime: The thread pool maintains the idle time allowed by threads

unit: The unit of idle time allowed by thread pool maintenance threads

workQueue: buffer queue used by the thread pool

handler: The thread pool's handling policy for rejected tasks

A task is added to the thread pool via the execute(Runnable) method. A task is an object of type Runnable, and the execution method of the task is the run() method of the object of type Runnable.

When a task is added to the thread pool via the execute(Runnable) method:

If the number of threads in the thread pool is less than the corePoolSize at this time, even if the threads in the thread pool are idle, a new thread will be created to handle the added task.

If the number of threads in the thread pool equals corePoolSize, but the buffer queue workQueue is not full, then the task is placed in the buffer queue.

If the number of threads in the thread pool is greater than corePoolSize, the buffer queue workQueue is full, and the number of threads in the thread pool is less than maximumPoolSize, a new thread is created to process the added task.

4. If the number of threads in the thread pool is greater than corePoolSize, the buffer queue is full, and the number of threads in the thread pool is equal to maximumPoolSize, then the task is handled by the policy specified by the handler.

The priority for processing tasks is:

Core thread corePoolSize, task queue workQueue, maximum thread maximumPoolSize, if all three are full, use handler to handle rejected tasks.

When the number of threads in the thread pool is greater than the corePoolSize, the thread is terminated if the idle time exceeds keepAliveTime. This allows the thread pool to dynamically adjust the number of threads in the pool.

The optional parameters of unit are several static properties in java.util.concurrent.TimeUnit:

NANOSECONDS

MICROSECONDS

MILLISECONDS

SECONDS

workQueue I often use: java.util.concurrent.ArrayBlockingQueue

The handler has four options:

1、ThreadPoolExecutor.AbortPolicy()

java.util.concurrent.RejectedExecutionException thrown

2、ThreadPoolExecutor.CallerRunsPolicy()

Try adding the current task again, it will automatically repeat the execute() method

3、ThreadPoolExecutor.DiscardOldestPolicy()

Abandon old tasks

4、ThreadPoolExecutor.DiscardPolicy()

Abandon the current mission

Examples:

import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;public class TestThreadPool {private static int produceTaskSleepTime = 2;private static int produceTaskMaxNumber = 10;public static void main(String[] args) { //construct a thread pool ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2, 4, 3, TimeUnit.SECONDS, new ArrayBlockingQueue(3), new ThreadPoolExecutor.DiscardOldestPolicy()); for (int i = 1; i

Tags: Thread task process quantity method team member policy queue team time buffer two object data method idle type work adjustment difference Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Information MariaDB NVidia Shulou Tech Info