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

How to evaluate how many threads need to be set up in a thread pool

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to evaluate how many threads need to be set in a thread pool". In daily operations, I believe many people have doubts about how to evaluate how many threads need to be set in a thread pool. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful to answer the doubts of "how to evaluate how many threads need to be set in a thread pool". Next, please follow the editor to study!

1. Basic working principle and interview guide of thread pool

1.1 Core properties of the java thread pool

The core properties of JAVA thread pools are as follows:

Int corePoolSize

Number of core threads

Int maximumPoolSize

Maximum number of threads in the thread pool

Long keepAliveTime

The amount of time a thread stays active

TimeUnit unit

The time unit of the keepAliveTime

BlockingQueue

< Runnable >

WorkQueue

Task squeeze queue

ThreadFactory threadFactory

Thread creates a factory class

RejectedExecutionHandler handler

Rejection strategy

1.2 Thread creation process when submitting a task to the thread pool

So how does the thread pool create threads when the user submits a task to the thread pool?

First of all, the thread pool will determine whether the currently created thread is less than corePoolSize (number of core threads). If so, regardless of whether the created thread is idle or not, it will choose to create a new thread to perform the task until the created thread is equal to the number of core threads.

When the number of threads created in the thread pool is equal to the number of core threads, and the user continues to submit tasks to the thread pool, it will first determine whether the task queue is full:

1) if the task queue is not full, put the task in the queue.

2) if the task queue is full, it is determined whether the current number of threads exceeds the maximum number of threads, if not, a new thread is created to perform the task, and if the maximum number of threads created by the thread pool, etc., the reject policy is executed.

Warm tip: so if the thread pool uses an unbounded queue, the maximum number of threads will become meaningless.

1.3 rejection strategy and usage scenarios of thread pool

JUC provides the following rejection policies by default:

AbortPolicy

Reject, throw RejectedExecutionException directly, default value.

CallerRunsPolicy

The run method of the task is run directly by the calling thread, that is, asynchronous to synchronous.

DiscardOldestPolicy

Discards the first task in the task queue.

DiscardPolicy

If you refuse, you will not carry it out, "when there is nothing to do with personnel".

The condition triggered by the reject policy: the thread pool can only be triggered when the thread pool uses a bounded task queue, when the queue is full and the thread created by the thread pool has reached the maximum allowed thread pool.

By default, AbortPolicy is usually used.

The conversion of CallerRunsPolicy asynchronism to synchronization does not make much sense in the case of rejection, and does not come up with a suitable scenario, because when you need to execute the rejection policy, the processing is already slow, and then synchronizing the execution of the task will only increase the load on the server, which is not conducive to recovery.

DiscardOldestPolicy is a strategy that is usually used to record tracks. It's okay to lose point data occasionally, but it is hoped that the latest data can be saved.

The DiscardPolicy policy, which is usually used to print logs asynchronously, simply ignores non-execution and expects to save old data.

1.4 how to select a blocking queue

Ali's internal open source specification explicitly prohibits the use of unbounded queues. If unbounded queues are used, tasks will be submitted to the thread pool without restrictions, which may cause memory overflow.

If you use an unbounded queue, the maximum number of threads parameter will be invalidated because more threads than the core thread will never be created.

1.5 what is the practical use of a thread pool factory

ThreadFactory threadFactory, thread pool factory, when using thread pool, it is highly recommended to use a self-defined thread factory, so that you can name the threads in the thread pool and quickly identify the corresponding threads when you use the jsatck command to view the thread stack.

1.6 the role of the keepAliveTime parameter

KeepAliveTime: in popular terms, this parameter represents the maximum idle time of a thread, that is, the amount of time that a thread can survive if it is not executing a task.

By default, this parameter is only for threads that exceed the number of core threads (corePoolSize). By setting allowCoreThreadTimeOut to true, the number of core threads will also be turned off because they are idle.

2. How to set the appropriate thread for the thread pool

Currently, according to some of the open source frameworks I've seen, the number of threads is usually set according to the type of application: IO-intensive and CPU-intensive.

IO-intensive is usually set to 2n+1, where n is the number of CPU cores

CPU-intensive is usually set to naugh1.

The actual situation is often much more complicated and will not be set according to this. The above formula is usually suitable for framework classes. For example, the underlying communication framework such as netty,dubbo is usually set with reference to the above standards.

About how to set up the right threads for a thread pool in actual business development?

In fact, for IO-intensive applications, there is a formula on the Internet: number of threads = number of CPU cores / (1-blocking coefficient)

The concept of blocking coefficient is introduced, which is generally between 0.8 and 0.9.

In our business development, basically are IO-intensive, because often to operate the database, access redis,es and other storage components, involving disk IO, network IO.

So what kind of scenario is CPU-intensive? Pure computing, such as calculating the number of digits of pi, is basically out of our reach.

IO-intensive, you can consider setting more threads, the main purpose is to increase the concurrency of IO. CPU-intensive is not suitable to set too many threads, because it will cause thread switching and lose performance.

Next, let's use an actual scenario to show how to set the number of threads.

A MQ consumer is deployed on a 4C8G machine, and in the implementation of RocketMQ, the consumer also uses a thread pool to consume threads, so how to set the number of threads?

If according to the formula of 2n + 1, the number of threads is set to 9, but in our practice, we find that increasing the number of threads will significantly improve the message processing capacity, indicating that 2n + 1 is not suitable for business scenarios.

If the number of threads applied = number of CPU cores / (1-blocking coefficient) blocking factor is 0.8, the number of threads is 20. The blocking coefficient is 0.9, and the number of threads is about 40. I think so.

If we find that the operation of the database is time-consuming, we can continue to increase the blocking coefficient, thus increasing the number of threads.

So how do we judge the need to add more threads? In fact, you can check the thread stack of the process with the jstack command. If you find that most of the threads in the thread pool are waiting for the acquisition task, it means that the threads are sufficient, as shown in the following figure:

If most of the threads are running, you can continue to increase the number of threads appropriately.

At this point, the study on "how to evaluate how many threads need to be set in a thread pool" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Development

Wechat

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

12
Report