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 solve the problem of thread pool monitoring based on Spring Boot

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to solve the problem of thread pool monitoring based on Spring Boot". In daily operation, I believe many people have doubts about how to solve the problem of thread pool monitoring based on Spring Boot. Xiaobian consulted all kinds of materials and sorted out simple and easy to use operation methods. I hope to answer the doubts of "how to solve the problem of thread pool monitoring based on Spring Boot"! Next, please follow the small series to learn together!

Why thread pool monitoring is needed

Java thread pool as the most commonly used concurrent tool, I believe everyone is familiar, but are you really sure to use the right? The famous Ali Java code specification requires us to quickly create thread pools without using Executors, but does it not have problems to discard Executors and create thread pools in other ways? In essence, for us, the running process of the thread pool itself is a black box. When we have no way to understand the running state in the thread pool, there is no way to judge and warn in time when there is a problem. In the face of this black box operation must be transparent through monitoring, so that we can better use the good thread pool. Therefore, the thread pool must be monitored.

How to do thread pool monitoring

For how to do monitoring, the essence is to involve three points, namely data acquisition, data storage and display of the market. Next, we will talk about these three points.

data acquisition

What data to collect, for us to collect is the black box data, what is the black box data of the thread pool, in fact, the whole process of thread processing, in the whole process, we can get data through the seven methods in ThreadPoolExecutor, through these seven methods to collect data can make the execution process of the thread pool transparent.

getCorePoolSize(): Gets the number of core threads;

getMaximumPoolSize: Gets the maximum number of threads;

getQueue(): Get the blocking queue in the thread pool, and get the queue length, number of elements, etc. through the methods in the blocking queue;

getPoolSize(): Gets the number of worker threads in the thread pool (including core threads and non-core threads);

getActiveCount(): Gets the number of active threads, i.e. threads executing tasks;

getLargestPoolSize(): Gets the maximum number of worker threads ever visited by the thread pool;

getTaskCount(): Gets the total number of tasks completed and in progress in history;

In addition to the processes we've seen, there are three hook functions available in Executor,

beforeExecute(): a method that the Worker thread calls before executing a task;

afterExecute(): Method called after the Worker thread executes the task;

terminated(): method called before the thread pool changes from running to terminated;

beforeExecute and afterExecute can be understood as using Aop to monitor thread execution time, so that we can monitor the overall running time of each thread, terminated can be understood as monitoring when the thread is closed, so that we can obtain all the data collected to the thread pool life cycle as a whole.

Data storage and display of large plates

For storage, we are more suitable for the use of time series database, in addition, now many mature monitoring products can meet our requirements for large screen display, here recommended under the United States Cat and Prometheus, here do not explain, we can choose according to their own company's monitoring products, for different solutions to take the form of storage will be some differences, even their own can customize to achieve a function, anyway, the difficulty is not big.

Further expansion and reflection

In actual project development we encounter the following scenarios:

Different services use the same thread pool, so if a service blocks, it will affect all services in the overall shared thread pool, triggering the thread pool rejection policy;

Traffic suddenly increases, you need to dynamically adjust the parameters of the thread pool, this time can not restart;

For these two scenarios, we think deeply about thread pools again:

How to configure thread pool parameters reasonably;

How to dynamically adjust thread pool parameters;

How to isolate thread pools between different services;

How to configure thread pool parameters reasonably

About this question interview is often asked, I can only say that this question is a pit at the beginning, for CPU-intensive and I/O-intensive, thread pool parameters are different design, also can not follow a few formulas can be done, of course, you can refer to, I think for thread pool reasonable parameter configuration is after many adjustments, even increase and decrease of business will affect some parameters, I do not recommend that you endorse CPU-intensive is N+1 every day, Non-CPU intensive is 2N, so we prefer to see thread pool dynamically configured.

How to dynamically adjust thread pool parameters

As for how to dynamically adjust the thread pool, or return to the solution of our scenario problem, the core of traffic surge is to improve the processing speed of the thread pool. There are two ways to improve the processing speed of the thread pool. One is to speed up the processing of the business, that is, the consumption is fast. Obviously, it is difficult for us to change this kind of business in operation. This can be used as the focus of the disk recovery. Another is to increase the number of consumers. The focus of increasing consumers is to adjust the number of core threads and the number of non-core threads.

In this thinking, this time we need to look at the ThreadPoolExecutor thread pool source code, first look at the beginning of the definition of variables, through the design of variables we will find that the master is the master, the master through AtomicInteger modified ctl variables, high 3 bits stored in the thread pool state, low 29 stores the number of threads, through a variable to complete two things, complete the state judgment and limit the maximum number of threads. A HashSet is used to store a reference to Worker, which inherits AbstractQueuedSynchronizer and implements an exclusive lock that cannot be flushed to ensure thread safety.

//Used to mark thread pool status (upper 3 bits), thread count (lower 29 bits) private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));//work status stored in the upper 3 bits private static final int COUNT_BITS = Integer.SIZE - 3;//maximum number of threads that can be expressed private static final int CAPACITY = (1

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