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

What is the origin of thread pool

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

Share

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

What is the origin of the thread pool, I believe that many inexperienced people are helpless about this, this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Why do I need a thread pool?

At present, most network servers, including WEB servers and database servers, have a common point, that is, they must process a large number of connection requests per unit time, but the processing time is relatively short. The server model used in traditional multithreaded scenarios is a "create, destroy" policy. If the task submitted to the thread takes a short time to execute and executes frequently, the server will constantly be in the state of creating threads and destroying threads.

thread execution

T1: Thread creation time

T2: thread execution time, including synchronization time of threads

T3: Time to thread destruction

Percentage of overhead of thread itself:

(T1+T3) / (T1+T2+T3)

When T2 is small, then the overhead of the thread itself will have a big impact, so to solve this problem, the thread pool appears

Thread pools use pre-creation technology, immediately after the application starts, a certain number of threads (corePoolSize) will be created and placed in the idle queue. These threads are blocking, do not consume CPU, but take up less memory space. When a task arrives, the buffer selects an idle thread and passes the task to run in that thread. When all corePoolSize threads are processing tasks, the buffer pool automatically creates a certain number (max. maximumPoolSize - corePoolSize) of new threads to process more tasks. The thread does not exit after the task is completed, but continues to stay in the pool waiting for the next task. When the system is relatively idle (thread idle time exceeds keepAliveTime), the thread pool automatically destroys some threads and recovers system resources. Based on this pre-creation technique, the thread pool distributes the cost of thread creation and destruction to specific tasks, and the more times each task executes, the less the cost of thread itself is shared by each task.

When is thread pool used

1. When T2 is small (short connection)

2. The number of tasks to be handled is large

Benefits of thread pools

1. Reuse existing threads, reduce the overhead of object creation and death, and have good performance.

2. It can effectively control the maximum number of concurrent threads, improve the utilization of system resources, and avoid excessive resource competition and blocking.

3, provide timed execution, regular execution, single thread, concurrent number control and other functions

Rational allocation of thread pool

1. CPU-intensive tasks need to squeeze CPU as much as possible. The reference value can be set to CPU +1.

2, IO-intensive tasks, IO-intensive tasks, the reference value can be set to 2*N CPU

ThreadPoolExecutor parameter

corePoolSize: Number of core threads

maximumPoolSize: Maximum number of threads

workQueue: blocking queue, storing tasks waiting to be executed, very important, will have an important impact on the thread pool running process

keepAliveTime: How long does the thread stay when no task is executing?

unit: keepAliveTime

threadFactory: thread factory, used to create threads

rejectHandler: Policy when refusing to process a task

method

execute()

Submit tasks to thread pool for execution

submit()

Submit a task and return execution results execute+Future

shutdown()

Close thread pool and wait for tasks to finish (no new tasks will be accepted, but tasks in blocking queue will finish)

shutdownNow()

Close thread pool without waiting for tasks to finish

getTaskCount()

Total number of executed and unexecuted tasks in the thread pool

getCompletedTaskCount()

Total number of tasks completed

getPoolSize()

The current number of threads in the thread pool

getActiveCount()

Number of threads executing tasks in the current thread pool

Executive Framework Interface Diagram

The following is an interface diagram from Baidu, which describes the thread pool related interfaces.

After reading the above content, do you know what the origin of thread pool is? If you still want to learn more skills or want to know more related content, welcome to pay attention to the industry information channel, thank you for reading!

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