In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
How to implement the above springboot2.0 scheduler configuration thread pool, I believe that many inexperienced people do not know what to do. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
When we open the spring task scheduler with @ EnableScheduling, we find that the scheduler is configured to be single-threaded by default.
Second, open the annotation and find its configuration information in this SchedulingConfiguration class. It is not difficult to find that the default configuration of the scheduler is the following code, and the thread pool is single-threaded.
Protected void scheduleTasks () {if (this.taskScheduler = = null) {this.localExecutor = Executors.newSingleThreadScheduledExecutor (); this.taskScheduler = new ConcurrentTaskScheduler (this.localExecutor);} if (this.triggerTasks! = null) {for (TriggerTask task: this.triggerTasks) {addScheduledTask (scheduleTriggerTask (task));} if (this.cronTasks! = null) {for (CronTask task: this.cronTasks) {addScheduledTask (scheduleCronTask (task)) } if (this.fixedRateTasks! = null) {for (IntervalTask task: this.fixedRateTasks) {addScheduledTask (scheduleFixedRateTask (task));}} if (this.fixedDelayTasks! = null) {for (IntervalTask task: this.fixedDelayTasks) {addScheduledTask (scheduleFixedDelayTask (task));}
How do I change this configuration?
If you want to change the configuration, you only need the following core code
Package com.ccbobe.common.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.SchedulingConfigurer;import org.springframework.scheduling.config.ScheduledTaskRegistrar;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.ScheduledThreadPoolExecutor;@EnableScheduling@Configurationpublic class SchedulerConfig implements SchedulingConfigurer {@ Bean public ScheduledExecutorService concurrentTaskScheduler () {ScheduledThreadPoolExecutor executorService = new ScheduledThreadPoolExecutor (20); executorService.setMaximumPoolSize (20); executorService.setRejectedExecutionHandler (new ScheduledThreadPoolExecutor.CallerRunsPolicy ()); return executorService @ Override public void configureTasks (ScheduledTaskRegistrar taskRegistrar) {taskRegistrar.setScheduler (concurrentTaskScheduler ());}}
Among them, Scheduler supports two kinds, which are TaskScheduler and ScheduledExecutorService
/ * Set the {@ link TaskScheduler} to register scheduled tasks with, or a * {@ link java.util.concurrent.ScheduledExecutorService} to be wrapped as a * {@ code TaskScheduler}. * / public void setScheduler (@ Nullable Object scheduler) {if (scheduler = = null) {this.taskScheduler = null;} else if (scheduler instanceof TaskScheduler) {this.taskScheduler = (TaskScheduler) scheduler;} else if (scheduler instanceof ScheduledExecutorService) {this.taskScheduler = new ConcurrentTaskScheduler (ScheduledExecutorService) scheduler));} else {throw new IllegalArgumentException ("Unsupported scheduler type:" + scheduler.getClass ());}}
After completing the above configuration, spring task can be run in a multithreaded environment.
After reading the above, have you mastered how to implement the thread pool configuration of the above springboot2.0 scheduler? 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!
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.