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 > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how springboot initializes the general thread pool". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "springboot how to initialize the general thread pool".
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;/*** @ description: general thread pool, which is used to perform write operations asynchronously without affecting the main thread * / @ Configuration@EnableAsyncpublic class InitThread {/ / the minimum number of threads maintained by the thread pool private static final int CORE_POOL_SIZE = 10 / / maximum number of threads maintained by the thread pool private static final int MAX_POOL_SIZE = 50; / / cache queue private static final int QUEUE_CAPACITY = 10; / / allowed idle time private static final int KEEP_ALIVE = 60; @ Bean public Executor myExecutor () {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor (); executor.setCorePoolSize (CORE_POOL_SIZE); executor.setMaxPoolSize (MAX_POOL_SIZE) Executor.setQueueCapacity (QUEUE_CAPACITY); executor.setThreadNamePrefix ("executor-"); / * * using this strategy, if adding to the thread pool fails, the main thread will execute the task on its own and will not wait for threads in the thread pool to execute * / executor.setRejectedExecutionHandler (new ThreadPoolExecutor.CallerRunsPolicy ()); executor.setKeepAliveSeconds (KEEP_ALIVE); executor.initialize () Return executor;}}
Deny Policy RejectedExecutionHandler
AbortPolicy: this policy is the default policy for thread pools. When using this strategy, if the thread pool queue is full, drop the task and throw a RejectedExecutionException exception public void rejectedExecution (Runnable r, ThreadPoolExecutor e) {/ / do nothing. Throw an exception throw new RejectedExecutionException directly ("Task" + r.toString () + "rejected from" + e.toString ()) } DiscardPolicy: this strategy and the slient version of AbortPolicy, if the thread pool queue is full, the task will be dropped without any exceptions. Public void rejectedExecution (Runnable r, ThreadPoolExecutor e) {/ / is an empty method} DiscardOldestPolicy: this strategy is literally easy to understand, discarding the oldest. That is, if the queue is full, the first task to enter the queue will be deleted to make room, and then try to join the queue. Because the queue is in and out at the end of the queue, the head element is the oldest, so every time you remove the opposite element and then try to join the team. Public void rejectedExecution (Runnable r, ThreadPoolExecutor e) {if (! e.isShutdown ()) {/ / remove the line header element e.getQueue () .poll (); / / try to join the team again e.execute (r) }} CallerRunsPolicy: with this strategy, if adding to the thread pool fails, the main thread will execute the task on its own and will not wait for the thread in the thread pool to execute. Like a quick-tempered person, I can't wait for anyone else to do it, so I might as well do it myself. Public void rejectedExecution (Runnable r, ThreadPoolExecutor e) {if (! e.isShutdown ()) {/ / directly execute the run method r.run ();}} Custom: just implement the RejectedExecutionHandler interface and implement the rejectedExecution method. The specific logic is defined in the rejectedExecution method to OK public class MyRejectPolicy implements RejectedExecutionHandler {public void rejectedExecution (Runnable r, ThreadPoolExecutor executor) {/ / Sender is my Runnable class, in which there are message fields if (r instanceof Sender) {Sender sender = (Sender) r; / directly print System.out.println (sender.getMessage ()) Thank you for your reading. The above is the content of "how springboot initializes the general thread pool". After the study of this article, I believe you have a deeper understanding of how springboot initializes the general thread pool, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.