SpringBoot多線程環(huán)境下,解決多個(gè)定時(shí)器沖突問(wèn)題
點(diǎn)擊關(guān)注公眾號(hào),實(shí)用技術(shù)文章及時(shí)了解
來(lái)源:blog.csdn.net/cssnnd/article/details/108328942
戰(zhàn)術(shù)分析:

實(shí)際開(kāi)發(fā)項(xiàng)目中一定不止一個(gè)定時(shí)器,很多場(chǎng)景都需要用到,而多個(gè)定時(shí)器帶來(lái)的問(wèn)題 : 就是如何避免多個(gè)定時(shí)器的互相沖突
使用場(chǎng)景 :
我們的訂單服務(wù),一般會(huì)有一個(gè)待支付訂單,而這個(gè)待支付訂單是有時(shí)間限制的,比如阿里巴巴的訂單是五天,淘寶訂單是一天,拼多多訂單是一天,美團(tuán)訂單是15分鐘…
基金系統(tǒng)中,如何同時(shí)更新多個(gè)存儲(chǔ)分區(qū)中的基金信息…
總的來(lái)說(shuō),實(shí)際開(kāi)發(fā)中定時(shí)器需要解決多個(gè)定時(shí)器同時(shí)并發(fā)的問(wèn)題,也要解決定時(shí)器之間的沖突問(wèn)題
問(wèn)題不大,說(shuō)到并發(fā)那就離不開(kāi)多線程了…慢慢看看就懂了
問(wèn)題場(chǎng)景重現(xiàn) :


我們清晰的看到執(zhí)行結(jié)果都是scheduling-1
就此可以判定,Springboot定時(shí)器默認(rèn)的是單線程的
但是問(wèn)題就來(lái)了,如果在線程爭(zhēng)奪資源后,某個(gè)線程需要比較長(zhǎng)時(shí)間才能執(zhí)行完,那其他的定時(shí)器怎么辦,都只能進(jìn)入等待狀態(tài),時(shí)間越久,累計(jì)等待的定時(shí)器越多,這就容易引起雪崩…
其實(shí)只需要添加一個(gè)配置類然后加注解就可以解決問(wèn)題了
添加注解

具體代碼如下 :
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component
public class SchedulerTaskController {
private Logger logger= LoggerFactory.getLogger(SchedulerTaskController.class);
private static final SimpleDateFormat dateFormat=new SimpleDateFormat("HH:mm:ss");
private int count=0;
@Scheduled(cron="*/6 * * * * ?")
@Async("threadPoolTaskExecutor")
public void process(){
logger.info("英文:this is scheduler task runing "+(count++));
}
@Scheduled(fixedRate = 6000)
@Async("threadPoolTaskExecutor")
public void currentTime(){
logger.info("中文:現(xiàn)在時(shí)間"+dateFormat.format(new Date()));
}
}
配置類如下 :

具體代碼如下 :
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;
/**使用多線程的時(shí)候,往往需要?jiǎng)?chuàng)建Thread類,或者實(shí)現(xiàn)Runnable接口,如果要使用到線程池,我們還需要來(lái)創(chuàng)建Executors,
* 在使用spring中,已經(jīng)給我們做了很好的支持。只要要@EnableAsync就可以使用多線程
* 通過(guò)spring給我們提供的ThreadPoolTaskExecutor就可以使用線程池。*/
//@Configuration 表示該類是一個(gè)配置類
@Configuration
@EnableAsync
//所有的定時(shí)任務(wù)都放在一個(gè)線程池中,定時(shí)任務(wù)啟動(dòng)時(shí)使用不同都線程。
public class TaskScheduleConfig {
private static final int corePoolSize = 10; // 默認(rèn)線程數(shù)
private static final int maxPoolSize = 100; // 最大線程數(shù)
private static final int keepAliveTime = 10; // 允許線程空閑時(shí)間(單位:默認(rèn)為秒),十秒后就把線程關(guān)閉
private static final int queueCapacity = 200; // 緩沖隊(duì)列數(shù)
private static final String threadNamePrefix = "it-is-threaddemo-"; // 線程池名前綴
@Bean("threadPoolTaskExecutor") // bean的名稱,默認(rèn)為首字母小寫(xiě)的方法名
public ThreadPoolTaskExecutor getDemoThread(){
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(keepAliveTime);
executor.setKeepAliveSeconds(queueCapacity);
executor.setThreadNamePrefix(threadNamePrefix);
//線程池拒絕任務(wù)的處理策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//初始化
executor.initialize();
return executor;
}
}
然后我們可以很清晰地看到

如上,也就解決了用多線程解決Springboot多定時(shí)器沖突的問(wèn)題

怎么接私活?這個(gè)渠道你100%有用!請(qǐng)收藏!
喜歡文章,點(diǎn)個(gè)在看


