Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to parse ThreadPoolExecutor

Shulou Source: shulou.com Published: 2022-06-01 12:02:36 09月22日 Update

This article will explain in detail how to parse ThreadPoolExecutor. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

The thread pool class is java.util.concurrent.ThreadPoolExecutor, and the common construction methods are:

ThreadPoolExecutor (intcorePoolSize, int maximumPoolSize

LongkeepAliveTime, TimeUnit unit, BlockingQueueworkQueue

RejectedExecutionHandlerhandler)

CorePoolSize: the minimum number of threads maintained by the thread pool

MaximumPoolSize: the maximum number of threads maintained by the thread pool

KeepAliveTime: thread pool maintains the idle time allowed by threads

Unit: the thread pool maintains the unit of idle time allowed by threads.

WorkQueue: the buffer queue used by the thread pool

Handler: thread pool strategy for rejecting tasks

A task is added to the thread pool through the execute (Runnable) method. The 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 to be added to the thread pool through the execute (Runnable) method:

1. If the number of threads in the thread pool is less than corePoolSize at this time, even if all the threads in the thread pool are idle, create new threads to handle the added tasks.

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

3. If the number of threads in the pool is greater than corePoolSize, the buffer queue workQueue is full, and the number of threads in the pool is less than maximumPoolSize, create new threads to handle the added tasks.

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

The priority of processing tasks is:

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

When the number of threads in the thread pool is greater than corePoolSize, if a thread is idle longer than keepAliveTime, the thread will be terminated. This allows the thread pool to dynamically adjust the number of threads in the pool.

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

NANOSECONDS

MICROSECONDS

MILLISECONDS

SECONDS

WorkQueue I often use is: java.util.concurrent.ArrayBlockingQueue

Handler has four options:

1. ThreadPoolExecutor.AbortPolicy ()

Throw a java.util.concurrent.RejectedExecutionException exception

2. ThreadPoolExecutor.CallerRunsPolicy ()

Try adding the current task again, and he will automatically repeat the call to the execute () method

3. ThreadPoolExecutor.DiscardOldestPolicy ()

Abandon the old task

4. ThreadPoolExecutor.DiscardPolicy ()

Abandon the current task

Example:

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 vpn Redmi Xiaomi Huawei MySQL