Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to configure Thread Pool in springboot

Shulou Source: shulou.com Published: 2022-06-02 10:39:45 10月30日 Update

It is believed that many inexperienced people have no idea about how to configure thread pool in springboot. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

1. Add configimport lombok.extern.slf4j.Slf4j;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.annotation.EnableAsync;import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import java.util.concurrent.Executor;import java.util.concurrent.ThreadPoolExecutor;@Configuration@EnableAsync@Slf4jpublic class ExecutorConfig {@ Bean ("taskExecutor") public Executor asyncServiceExecutor () {log.info ("- create thread pool--") ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor (); / / number of core threads executor.setCorePoolSize (10); / / maximum number of threads executor.setMaxPoolSize (20); / / queue size executor.setQueueCapacity (200); / / configure the name prefix executor.setThreadNamePrefix ("async-method-") of threads in the thread pool / * rejection-policy: how to deal with the new task thread pool's strategy for rejecting tasks when pool has reached max size: the CallerRunsPolicy policy is used here. When the thread pool has no processing capacity, this policy runs the rejected task directly in the calling thread of the execute method. If the executor has been closed, the task * / executor.setRejectedExecutionHandler (new ThreadPoolExecutor.CallerRunsPolicy ()) will be discarded; / / when the thread pool is closed, wait for all tasks to be completed before continuing to destroy other Bean executor.setWaitForTasksToCompleteOnShutdown (true). / / set the waiting time of the task in the thread pool, and force it to be destroyed if it has not been destroyed, so as to ensure that the application can be closed eventually instead of blocking the executor.setAwaitTerminationSeconds (60); / / perform initialization executor.initialize (); return executor;}} 2. Define an asynchronous method / * Asynchronous invocation test interface * / public interface IAsyncService {void testAsyncMethod () throws Exception;} @ Service@Slf4jpublic class AsyncServiceImpl implements IAsyncService {/ / where @ bean in taskExecutor and config are consistent @ Async ("taskExecutor") @ Override public void testAsyncMethod () throws Exception {log.info ("Asynchronous method, go--"); long start = System.currentTimeMillis (); Thread.sleep (5000) Long end = System.currentTimeMillis (); log.info ("Asynchronous method, end:" + (end-start) + "millisecond");}}

3. test

Public Result test () {System.out.println ("1 ="); System.out.println ("2 ="); try {asyncService.testAsyncMethod ();} catch (Exception e) {e.printStackTrace () } System.out.println ("3 ="); System.out.println ("4 ="); return Result.ok ("Test succeeded!") ;}

The console prints as follows:

Note:

If an error is found in the startup project:

Solution: add configuration to yml

Spring: main: allow-bean-definition-overriding: true after reading the above, have you mastered how to configure thread pools in springboot? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Thread task method configuration time policy test content more problem handling maximum consistent success helpless for this prefix reason name remarks Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Docker Shulou Technology Apple MySQL