In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces springboot how to create a thread pool, the article is very detailed, has a certain reference value, interested friends must read it!
There are two ways for springboot to create thread pool 1. Create using static code block
The advantage of creating in this way is that the number of core threads is initialized only when the thread pool is used in contemporary code.
The specific code is as follows:
Public class HttpApiThreadPool {/ * * get the number of CPU of the current system * / static int cpuNums = Runtime.getRuntime () .availableProcessors (); / * * size of thread pool core pool * / private static int corePoolSize = 10; / * * maximum number of threads of thread pool * / private static int maximumPoolSize = cpuNums * 5; public static ExecutorService httpApiThreadPool = null / * static method * / static {System.out.println ("create threads:" + corePoolSize+ ", maximum number of threads:" + maximumPoolSize); / / set up 10 core threads and the number of thread requests exceeds 20, then enter the queue and wait httpApiThreadPool = new ThreadPoolExecutor (corePoolSize, maximumPoolSize, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue (100), new ThreadFactoryBuilder (). SetNameFormat ("PROS-%d"). Build ();}}
Usage
Public static void main (String [] args) {HttpApiThreadPool.httpApiThreadPool.execute (()-> System.out.println ("Test");}
Note:
1. Thread pools cannot be created using Executors's method, which is the result of a large number of production accidents
2.maximumPoolSize this program uses 5 times the number of cup, you can use it according to your actual situation
3.new ThreadFactoryBuilder (). SetNameFormat ("PROS-%d"). Build () gives each thread a name, which is convenient for debugging
two。 Using @ Configuration @ bean annotation, create @ Configurationpublic class TreadPoolConfig {private Logger logger = LoggerFactory.getLogger (TreadPoolConfig.class) when the program starts; / * * get the number of CPU of the current system * / int cpuNums = Runtime.getRuntime (). AvailableProcessors (); / * the size of the thread pool core pool * / private int corePoolSize = 10; / * * the maximum number of threads in the thread pool * / private int maximumPoolSize = cpuNums * 5 / * * Consumer queue threads * @ return * / @ Bean (value = "httpApiThreadPool") public ExecutorService buildHttpApiThreadPool () {logger.info ("TreadPoolConfig create threads:" + corePoolSize+ ", maximum threads:" + maximumPoolSize); ExecutorService pool = new ThreadPoolExecutor (corePoolSize, maximumPoolSize, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue (100), new ThreadFactoryBuilder () .setNameFormat ("PROS-%d"). Build (); return pool;}}
Usage
/ / inject @ Resource private TreadPoolConfig treadPoolConfig; / / call public void test () {treadPoolConfig.buildHttpApiThreadPool () .execute (()-> System.out.println ("tre"));}
Now the two thread pool creation methods have been introduced.
Springboot opens thread pool to define thread pool
The location defined in the subpackage or sibling directory of the startup class
Import lombok.Data;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.ThreadPoolExecutor;@Data@Configuration@EnableAsync / / Open asynchronous request public class ThreadPoolConfig {private static final int corePoolSize = 10; / / number of core threads (default number of threads) private static final int maxPoolSize = 100 / / maximum number of threads private static final int keepAliveTime = 10; / / allowed idle time (in seconds) private static final int queueCapacity = 500; / / number of buffer queues / * * default asynchronous thread pool * @ return * / @ Bean ("taskExecutor") public ThreadPoolTaskExecutor taskExecutor () {ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor () Pool.setThreadNamePrefix ("- global thread pool -"); pool.setCorePoolSize (corePoolSize); pool.setMaxPoolSize (maxPoolSize); pool.setKeepAliveSeconds (keepAliveTime); pool.setQueueCapacity (queueCapacity); / / run pool.setRejectedExecutionHandler (new ThreadPoolExecutor.CallerRunsPolicy ()) directly in the calling thread of the execute method / / initialize pool.initialize (); return pool;}} use
Directly annotate methods that require asynchronous execution
@ Async ("taskExecutor") above is all the content of the article "how springboot creates a thread pool". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.