springboot2.0以上調(diào)度器配置線程池的實(shí)現(xiàn)
一 我們使用@EnableScheduling 開啟spring task 調(diào)度器的時候,發(fā)現(xiàn)此調(diào)度器默認(rèn)配置為單線程的。
二 打開注解發(fā)現(xiàn)其配置信息在此SchedulingConfiguration類中。發(fā)現(xiàn)其創(chuàng)建了ScheduledTaskRegistrar類
研讀代碼不難發(fā)現(xiàn)調(diào)度器默認(rèn)配置是如下代碼,線程池為單線程的。
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)); } } }
如何改變此配置呢?
如果想改變其中配置則只需要如下核心代碼
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 @Configuration public 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()); } }
其中Scheduler 支持兩種,種分別是:TaskScheduler 和 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()); } }
完成以上配置,即可讓spring task 運(yùn)行在多線程環(huán)境中。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot + Vue 前后端分離項目如何踢掉已登錄用戶
這篇文章主要介紹了Spring Boot + Vue 前后端分離項目如何踢掉已登錄用戶,需要的朋友可以參考下2020-05-05詳解Spring?Security?捕獲?filter?層面異常返回我們自定義的內(nèi)容
Spring?的異常會轉(zhuǎn)發(fā)到?BasicErrorController?中進(jìn)行異常寫入,然后才會返回客戶端。所以,我們可以在?BasicErrorController?對?filter異常進(jìn)行捕獲并處理,下面通過本文給大家介紹Spring?Security?捕獲?filter?層面異常,返回我們自定義的內(nèi)容,感興趣的朋友一起看看吧2022-05-05Java AQS中CyclicBarrier回環(huán)柵欄的使用
這篇文章主要介紹了Java中的 CyclicBarrier詳解,CyclicBarrier沒有顯示繼承哪個父類或者實(shí)現(xiàn)哪個父接口, 所有AQS和重入鎖不是通過繼承實(shí)現(xiàn)的,而是通過組合實(shí)現(xiàn)的,下文相關(guān)內(nèi)容需要的小伙伴可以參考一下2023-02-02java實(shí)現(xiàn)題目以及選項亂序的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于java實(shí)現(xiàn)題目以及選項亂序的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Java不用算數(shù)運(yùn)算符來實(shí)現(xiàn)求和方法
我們都知道,Java的運(yùn)算符除了具有優(yōu)先級之外,還有一個結(jié)合性的特點(diǎn)。當(dāng)一個表達(dá)式中出現(xiàn)多種運(yùn)算符時,執(zhí)行的先后順序不僅要遵守運(yùn)算符優(yōu)先級別的規(guī)定,還要受運(yùn)算符結(jié)合性的約束,以便確定是自左向右進(jìn)行運(yùn)算還是自右向左進(jìn)行運(yùn)算,但是如果不用運(yùn)算符怎么求和呢2022-04-04Java HttpClient實(shí)現(xiàn)socks代理的示例代碼
這篇文章主要介紹了Java HttpClient 實(shí)現(xiàn) socks 代理的示例代碼,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-11-11