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 optimize the concurrent processing of data in Springboot thread pool

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about how to optimize the concurrent processing of data in the Springboot thread pool. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Step 1: configure the basic thread parameters first

You can put it in the application.propertes file or in your newly created config/ file directory, note: but you need to use @ PropertySource to load the configuration file.

# Asynchronous Thread configuration # configure Core threads async.executor.thread.core_pool_size = "configure maximum number of threads async.executor.thread.max_pool_size = 2" configure queue size async.executor.thread.queue_capacity = 9999 configure the name prefix async.executor.thread.name.prefix = async-service- of threads in the thread pool step 2: let Spring Boot load

To define how to create a ThreadPoolTaskExecutor, use @ Configuration and @ EnableAsync annotations to indicate that this is a configuration class and a configuration class for thread pools

@ Slf4j@EnableAsync@Configurationpublic class RCExecutorConfig {@ Value ("${async.executor.thread.core_pool_size}") private int corePoolSize; @ Value ("${async.executor.thread.max_pool_size}") private int maxPoolSize; @ Value ("${async.executor.thread.queue_capacity}") private int queueCapacity; @ Value ("${async.executor.thread.name.prefix}") private String namePrefix Bean (name = "asyncServiceExecutor") public Executor asyncServiceExecutor () {log.info ("start asyncServiceExecutor"); ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor (); / / ThreadPoolTaskExecutor executor = new VisiableThreadPoolTaskExecutor (); / / configure the number of core threads executor.setCorePoolSize (corePoolSize); / / configure the maximum number of threads executor.setMaxPoolSize (maxPoolSize) / / configure the queue size executor.setQueueCapacity (queueCapacity); / / configure the name prefix executor.setThreadNamePrefix (namePrefix) of threads in the thread pool / / rejection-policy: how to handle the new task when pool has reached max size / / CALLER_RUNS: instead of executing the task in the new thread, the caller's thread executes executor.setRejectedExecutionHandler (new ThreadPoolExecutor.CallerRunsPolicy ()); / / initializes executor.initialize (); return executor;}} step 3: create a service interface

Is an interface for asynchronous threads for easy testing

Public interface AsyncService {/ * execute asynchronous tasks * you can draw up * / void executeAsync () by adding parameters according to your needs;} step 4: write real-world classes

Asynchronize the services in the Service layer and add @ Async ("asyncServiceExecutor") to the executeAsync () method. The asyncServiceExecutor method is the method name in the previous RCExecutorConfig.java, indicating that the thread pool that the executeAsync method enters is created by the asyncServiceExecutor method.

For testing, I have added a scheduled task that uses corn expressions. (if you don't know, you can learn about it online.)

@ Slf4j@Servicepublic class AsyncServiceImpl implements AsyncService {@ Override @ Scheduled (cron = "* / 1 *?") @ Async ("asyncServiceExecutor") public void executeAsync () {log.info ("start executeAsync"); System.out.println ("Asynchronous thread performs time-consuming tasks such as bulk insertion"); log.info ("end executeAsync");} step 5: the test results are as follows

10 start executeAsync 32 start executeAsync 15. 004 [async-service-1]

Asynchronous threads perform time-consuming tasks such as bulk insert

10 end executeAsync 32 end executeAsync 15. 004 [async-service-1]

10 start executeAsync 32 Switzerland 16.003 [async-service-2] INFO c.a.a.service.impl.AsyncServiceImpl-start executeAsync

Asynchronous threads perform time-consuming tasks such as bulk insert

10 end executeAsync 32 rig 16.004 [async-service-2] INFO c.a.a.service.impl.AsyncServiceImpl-

10 start executeAsync 32 start executeAsync start executeAsync 17. 001 [async-service-3]

Asynchronous threads perform time-consuming tasks such as bulk insert

10 end executeAsync 32 end executeAsync end executeAsync 17. 001 [async-service-3]

10 start executeAsync 32 start executeAsync INFO c.a.a.service.impl.AsyncServiceImpl 18.002 [async-service-4]

Asynchronous threads perform time-consuming tasks such as bulk insert

10 end executeAsync 32 end executeAsync INFO c.a.a.service.impl.AsyncServiceImpl 18.003 [async-service-4]

10 start executeAsync 32 start executeAsync 19.002 [async-service-5]

Asynchronous threads perform time-consuming tasks such as bulk insert

10 end executeAsync 32 end executeAsync 19.003 [async-service-5]

10 start executeAsync 32 start executeAsync 20. 001 [async-service-6] start executeAsync

Asynchronous threads perform time-consuming tasks such as bulk insert

10 end executeAsync 32 Switzerland 20.002 [async-service-6] INFO c.a.a.service.impl.AsyncServiceImpl-end executeAsync

10 start executeAsync 32 start executeAsync 21.002 [async-service-7]

Asynchronous threads perform time-consuming tasks such as bulk insert

10 end executeAsync 32 end executeAsync 21.002 [async-service-7]

10 start executeAsync 32 start executeAsync 22. 004 [async-service-8] start executeAsync

Asynchronous threads perform time-consuming tasks such as bulk insert

10 end executeAsync 32 end executeAsync 22. 005 [async-service-8] end executeAsync

10 start executeAsync 32 start executeAsync 23. 001 [async-service-1] start executeAsync

Asynchronous threads perform time-consuming tasks such as bulk insert

10 end executeAsync 32 end executeAsync 23. 003 [async-service-1] end executeAsync

10 start executeAsync 32 Switzerland 24.003 [async-service-2] INFO c.a.a.service.impl.AsyncServiceImpl-start executeAsync

Asynchronous threads perform time-consuming tasks such as bulk insert

10 end executeAsync 32 Switzerland 24.003 [async-service-2] INFO c.a.a.service.impl.AsyncServiceImpl-end executeAsync

10 start executeAsync 32 start executeAsync 25. 001 [async-service-3] start executeAsync

Asynchronous threads perform time-consuming tasks such as bulk insert

10 end executeAsync 32 end executeAsync 25. 001 [async-service-3] end executeAsync

10 start executeAsync 32 26. 002 [async-service-4] Vol-start executeAsync

Asynchronous threads perform time-consuming tasks such as bulk insert

10 end executeAsync 32 26. 002 [async-service-4] Vol-end executeAsync

10 start executeAsync 32 async-service-5 27. 002 [async-service-5] Vol-start executeAsync

Asynchronous threads perform time-consuming tasks such as bulk insert

10 end executeAsync 32 async-service-5 27. 003 [async-service-5] Vol-end executeAsync

10 async-service-6 32 Frez 28.001 [async-service-6] start executeAsync

Asynchronous threads perform time-consuming tasks such as bulk insert

10 async-service-6 32 Frez 28.001 [async-service-6] end executeAsync

10 start executeAsync 32 Switzerland 29.001 [async-service-7] INFO c.a.a.service.impl.AsyncServiceImpl-start executeAsync

Asynchronous threads perform time-consuming tasks such as bulk insert

10 end executeAsync 32 Switzerland 29.002 [async-service-7] INFO c.a.a.service.impl.AsyncServiceImpl-end executeAsync

10 start executeAsync 32 async-service-8 30.001 [async-service-8] Vol-start executeAsync

Asynchronous threads perform time-consuming tasks such as bulk insert

10 end executeAsync 32 async-service-8 30.001 [async-service-8] Vol-end executeAsync

10 start executeAsync 32 async-service-1 31.001 [async-service-1] INFO c.a.a.service.impl.AsyncServiceImpl-start executeAsync

Asynchronous threads perform time-consuming tasks such as bulk insert

10 end executeAsync 32 async-service-1 31.001 [async-service-1] INFO c.a.a.service.impl.AsyncServiceImpl-end executeAsync

It's not over yet:

From the above log, we can find that [async-service-] has multiple threads, which has obviously been executed in our configured thread pool, indicating that each request responds quickly, while time-consuming operations are left to threads in the thread pool to execute asynchronously.

Another question is that although the thread pool has been used, it is still not clear what the thread pool was like, how many threads were executing and how many were waiting in the queue. So here I create a subclass of ThreadPoolTaskExecutor that prints the health of the current thread pool every time a thread is submitted.

Import lombok.extern.slf4j.Slf4j; @ Slf4jpublic class VisiableThreadPoolTaskExecutor extends ThreadPoolTaskExecutor {/ * / private static final long serialVersionUID =-3518460523928455463L; private void showThreadPoolInfo (String prefix) {ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor (); if (null = = threadPoolExecutor) {return } log.info ("{}, {}, taskCount [{}], completedTaskCount [{}], activeCount [{}], queueSize [{}]", this.getThreadNamePrefix (), prefix, threadPoolExecutor.getTaskCount (), threadPoolExecutor.getCompletedTaskCount (), threadPoolExecutor.getActiveCount (), threadPoolExecutor.getQueue (). Size ()) } @ Override public void execute (Runnable task) {showThreadPoolInfo ("1.do execute"); super.execute (task);} @ Override public void execute (Runnable task, long startTimeout) {showThreadPoolInfo ("2.do execute"); super.execute (task, startTimeout);} @ Override public Future submit (Runnable task) {showThreadPoolInfo ("1.do submit") Return super.submit (task);} @ Override public Future submit (Callable task) {showThreadPoolInfo ("2.do submit"); return super.submit (task);} @ Override public ListenableFuture submitListenable (Runnable task) {showThreadPoolInfo ("1.do submitListenable"); return super.submitListenable (task);} @ Override public ListenableFuture submitListenable (Callable task) {showThreadPoolInfo ("2.do submitListenable") Return super.submitListenable (task);}}

Secondly: modify the asyncServiceExecutor method of RCExecutorConfig.java

Change ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor () to ThreadPoolTaskExecutor executor = new VisiableThreadPoolTaskExecutor ()

Bean (name = "asyncServiceExecutor") public Executor asyncServiceExecutor () {log.info ("start asyncServiceExecutor"); / / ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor (); ThreadPoolTaskExecutor executor = new VisiableThreadPoolTaskExecutor (); / / configure the number of core threads executor.setCorePoolSize (corePoolSize); / / configure the maximum number of threads executor.setMaxPoolSize (maxPoolSize) / / configure the queue size executor.setQueueCapacity (queueCapacity); / / configure the name prefix executor.setThreadNamePrefix (namePrefix) of threads in the thread pool / / rejection-policy: how to handle the new task when pool has reached max size / / CALLER_RUNS: instead of executing the task in the new thread, the caller's thread executes executor.setRejectedExecutionHandler (new ThreadPoolExecutor.CallerRunsPolicy ()); / / initializes executor.initialize (); return executor;}

The test results are as follows:

Asynchronous threads perform time-consuming tasks such as bulk insert

10 async-service-5 41Rose 35.003 [async-service-5] INFO c.a.a.service.impl.AsyncServiceImpl-end executeAsync

10 sheduled-pool-1-thread-1 41 async-service-, 36.001 [sheduled-pool-1-thread-1] do submit,taskCount [5], completedTaskCount [5], activeCount [0], queueSize [0]

10RV 41RV 36.001 [async-service-6] INFO c.a.a.service.impl.AsyncServiceImpl-start executeAsync

Asynchronous threads perform time-consuming tasks such as bulk insert

10RV 41RV 36.002 [async-service-6] INFO c.a.a.service.impl.AsyncServiceImpl-end executeAsync

10 async-service-, 41 async-service-, 37.001 [sheduled-pool-1-thread-7] INFO c.a.a.e.t.VisiableThreadPoolTaskExecutor-async-service-, 2. Do submit,taskCount [6], completedTaskCount [6], activeCount [0], queueSize [0]

10start executeAsync 41VV 37.001 [async-service-7] INFO c.a.a.service.impl.AsyncServiceImpl-start executeAsync

Asynchronous threads perform time-consuming tasks such as bulk insert

10Vera 41VV 37.002 [async-service-7] INFO c.a.a.service.impl.AsyncServiceImpl-end executeAsync

10 sheduled-pool-1-thread-7 41 do submit,taskCount 38.002 [sheduled-pool-1-thread-7] INFO c.a.a.e.t.VisiableThreadPoolTaskExecutor-async-service-, 2. Do submit,taskCount [7], completedTaskCount [7], activeCount [0], queueSize [0]

10 async-service-8 41Rose 38.002 [async-service-8] INFO c.a.a.service.impl.AsyncServiceImpl-start executeAsync

Asynchronous threads perform time-consuming tasks such as bulk insert

10 async-service-8 41Rose 38.002 [async-service-8] INFO c.a.a.service.impl.AsyncServiceImpl-end executeAsync

10 sheduled-pool-1-thread-7 41 do submit,taskCount 39.001 [sheduled-pool-1-thread-7] INFO c.a.a.e.t.VisiableThreadPoolTaskExecutor-async-service-, 2. Do submit,taskCount [8], completedTaskCount [8], activeCount [0], queueSize [0]

10start executeAsync 41VRV 39.001 [async-service-1] INFO c.a.a.service.impl.AsyncServiceImpl-start executeAsync

Asynchronous threads perform time-consuming tasks such as bulk insert

10 async-service-1 41RV 39.002 [async-service-1] INFO c.a.a.service.impl.AsyncServiceImpl-end executeAsync

Async-service-, 40.003 [sheduled-pool-1-thread-7] INFO c.a.a.e.t.VisiableThreadPoolTaskExecutor-async-service-, 2. Do submit,taskCount [9], completedTaskCount [9], activeCount [0], queueSize [0]

10 async-service-2 41RV 40.003 [async-service-2] INFO c.a.a.service.impl.AsyncServiceImpl-start executeAsync

Asynchronous threads perform time-consuming tasks such as bulk insert

10 async-service-2 41RV 40.003 [async-service-2] INFO c.a.a.service.impl.AsyncServiceImpl-end executeAsync

Async-service-, 41.001 [sheduled-pool-1-thread-7] INFO c.a.a.e.t.VisiableThreadPoolTaskExecutor-async-service-, 2. Do submit,taskCount [10], completedTaskCount [10], activeCount [0], queueSize [0]

Start executeAsync 41.001 [async-service-3] INFO c.a.a.service.impl.AsyncServiceImpl-start executeAsync

Asynchronous threads perform time-consuming tasks such as bulk insert

End executeAsync 41.001 [async-service-3] INFO c.a.a.service.impl.AsyncServiceImpl-end executeAsync

10async-service-, 41do submit,taskCount 42.000 [sheduled-pool-1-thread-7] INFO c.a.a.e.t.VisiableThreadPoolTaskExecutor-async-service-, 2. Do submit,taskCount [11], completedTaskCount [11], activeCount [0], queueSize [0]

10start executeAsync 41Rose 42.000 [async-service-4] INFO c.a.a.service.impl.AsyncServiceImpl-start executeAsync

Asynchronous threads perform time-consuming tasks such as bulk insert

10end executeAsync 41Rose 42.000 [async-service-4] INFO c.a.a.service.impl.AsyncServiceImpl-end executeAsync

Async-service-, 43.001 [sheduled-pool-1-thread-7] INFO c.a.a.e.t.VisiableThreadPoolTaskExecutor-async-service-, 2. Do submit,taskCount [12], completedTaskCount [12], activeCount [0], queueSize [0]

10RV 41RV 43.002 [async-service-5] INFO c.a.a.service.impl.AsyncServiceImpl-start executeAsync

Asynchronous threads perform time-consuming tasks such as bulk insert

10RV 41RV 43.003 [async-service-5] INFO c.a.a.service.impl.AsyncServiceImpl-end executeAsync

10 sheduled-pool-1-thread-7 44.001 [sheduled-pool-1-thread-7] INFO c.a.a.e.t.VisiableThreadPoolTaskExecutor-async-service-, 2. Do submit,taskCount [13], completedTaskCount [13], activeCount [0], queueSize [0]

10start executeAsync 41RV 44.001 [async-service-6] INFO c.a.a.service.impl.AsyncServiceImpl-start executeAsync

Asynchronous threads perform time-consuming tasks such as bulk insert

10end executeAsync 41RV 44.001 [async-service-6] INFO c.a.a.service.impl.AsyncServiceImpl-end executeAsync

10 sheduled-pool-1-thread-7 41 do submit,taskCount 45.000 [sheduled-pool-1-thread-7] INFO c.a.a.e.t.VisiableThreadPoolTaskExecutor-async-service-, 2. Do submit,taskCount [14], completedTaskCount [14], activeCount [0], queueSize [0]

10 async-service-7 41RV 45.001 [async-service-7] INFO c.a.a.service.impl.AsyncServiceImpl-start executeAsync

Explanation: 14 tasks have been submitted, 14 tasks have been processed, and there are 0 tasks left in the queue.

10 sheduled-pool-1-thread-7 41 do submit,taskCount 45.000 [sheduled-pool-1-thread-7] INFO c.a.a.e.t.VisiableThreadPoolTaskExecutor-async-service-, 2. Do submit,taskCount [14], completedTaskCount [14], activeCount [0], queueSize [0]

Thank you for reading! This is the end of the article on "how to optimize the concurrent processing of data in Springboot thread pool". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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