Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction to thread pool class ThreadPoolExecutor

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report