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í)器沖突問題

        共 3303字,需瀏覽 7分鐘

         ·

        2021-12-18 16:17

        程序員的成長(zhǎng)之路
        互聯(lián)網(wǎng)/程序員/技術(shù)/資料共享?
        關(guān)注


        閱讀本文大概需要 2.8 分鐘。

        來自: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í)器沖突的問題

        推薦閱讀:

        一個(gè)員工的離職成本有多恐怖!

        SpringBoot + Redis:模擬 10w 人的秒殺搶單!

        互聯(lián)網(wǎng)初中高級(jí)大廠面試題(9個(gè)G)

        內(nèi)容包含Java基礎(chǔ)、JavaWeb、MySQL性能優(yōu)化、JVM、鎖、百萬并發(fā)、消息隊(duì)列、高性能緩存、反射、Spring全家桶原理、微服務(wù)、Zookeeper、數(shù)據(jù)結(jié)構(gòu)、限流熔斷降級(jí)......等技術(shù)棧!

        ?戳閱讀原文領(lǐng)??!? ? ? ? ? ? ? ??? ??? ? ? ? ? ? ? ? ? ?朕已閱?

        瀏覽 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>
            99热99 | 偷拍自拍视频在线 | 日皮视频免费观看 | 九九在线视频 | 菲律宾毛片 | www.狠狠| 亚洲精品久久久久 | 亚洲天堂AV在线观看 | 亚洲国产成人无码影院 | AAAA毛片 |