1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        SpringBoot多線程環(huán)境下,解決多個(gè)定時(shí)器沖突問題

        共 3121字,需瀏覽 7分鐘

         ·

        2021-12-17 23:43


        來源:blog.csdn.net/cssnnd/article/details/108328942

        戰(zhàn)術(shù)分析:


        實(shí)際開發(fā)項(xiàng)目中一定不止一個(gè)定時(shí)器,很多場(chǎng)景都需要用到,而多個(gè)定時(shí)器帶來的問題 : 就是如何避免多個(gè)定時(shí)器的互相沖突

        使用場(chǎng)景 :

        我們的訂單服務(wù),一般會(huì)有一個(gè)待支付訂單,而這個(gè)待支付訂單是有時(shí)間限制的,比如阿里巴巴的訂單是五天,淘寶訂單是一天,拼多多訂單是一天,美團(tuán)訂單是15分鐘…

        基金系統(tǒng)中,如何同時(shí)更新多個(gè)存儲(chǔ)分區(qū)中的基金信息…

        總的來說,實(shí)際開發(fā)中定時(shí)器需要解決多個(gè)定時(shí)器同時(shí)并發(fā)的問題,也要解決定時(shí)器之間的沖突問題

        問題不大,說到并發(fā)那就離不開多線程了…慢慢看看就懂了

        問題場(chǎng)景重現(xiàn) :

        我們清晰的看到執(zhí)行結(jié)果都是scheduling-1

        就此可以判定,Springboot定時(shí)器默認(rèn)的是單線程的

        但是問題就來了,如果在線程爭(zhēng)奪資源后,某個(gè)線程需要比較長(zhǎng)時(shí)間才能執(zhí)行完,那其他的定時(shí)器怎么辦,都只能進(jìn)入等待狀態(tài),時(shí)間越久,累計(jì)等待的定時(shí)器越多,這就容易引起雪崩…

        其實(shí)只需要添加一個(gè)配置類然后加注解就可以解決問題了

        添加注解

        具體代碼如下 :

        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接口,如果要使用到線程池,我們還需要來創(chuàng)建Executors,
        ?*?在使用spring中,已經(jīng)給我們做了很好的支持。只要要@EnableAsync就可以使用多線程
        ?*?通過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)為首字母小寫的方法名
        ????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í)器沖突的問題

        PS:因?yàn)楣娞?hào)平臺(tái)更改了推送規(guī)則,如果不想錯(cuò)過內(nèi)容,記得讀完點(diǎn)一下“在看”,加個(gè)“星標(biāo)”,這樣每次新文章推送才會(huì)第一時(shí)間出現(xiàn)在你的訂閱列表里。點(diǎn)“在看”支持我們吧!

        瀏覽 44
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            全网可操逼 | 免费播放片色情A片 | 小静好湿好紧太爽视频 | 少妇bbbb水多毛片人 | 女人做爰高潮叫床视频 | 瑜伽课外遇hd三级 | 一级黄色操逼片 | 国产成人无码精品久久久电影 | 一级a免一级a做免费线看内裤的 | 国产女人18高潮毛片 |