<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Springboot定時器多線程解決多個定時器沖突問題

          共 3067字,需瀏覽 7分鐘

           ·

          2022-01-22 12:52

          Springboot定時器多線程解決多個定時器沖突問題

          戰(zhàn)術分析

          戰(zhàn)術分析

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

          使用場景

          1. 我們的訂單服務,一般會有一個待支付訂單,而這個待支付訂單是有時間限制的,比如阿里巴巴的訂單是五天,淘寶訂單是一天,拼多多訂單是一天,美團訂單是15分鐘…
          2. 基金系統(tǒng)中,如何同時更新多個存儲分區(qū)中的基金信息…

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

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

          問題場景重現(xiàn)

          問題場景重現(xiàn)
          問題場景重現(xiàn)

          我們清晰的看到執(zhí)行結果都是scheduling-1就此可以判定,Springboot定時器默認的是單線程的。

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

          其實只需要添加一個配置類然后加注解就可以解決問題了

          添加注解

          添加注解

          具體代碼如下 :

          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)在時間"+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;

          /**使用多線程的時候,往往需要創(chuàng)建Thread類,或者實現(xiàn)Runnable接口,如果要使用到線程池,我們還需要來創(chuàng)建Executors,
          ?*?在使用spring中,已經(jīng)給我們做了很好的支持。只要要@EnableAsync就可以使用多線程
          ?*?通過spring給我們提供的ThreadPoolTaskExecutor就可以使用線程池。*/

          //@Configuration?表示該類是一個配置類
          @Configuration
          @EnableAsync
          //所有的定時任務都放在一個線程池中,定時任務啟動時使用不同都線程。
          public?class?TaskScheduleConfig?{
          ????private?static?final?int?corePoolSize?=?10;?????????//?默認線程數(shù)
          ????private?static?final?int?maxPoolSize?=?100;???????//?最大線程數(shù)
          ????private?static?final?int?keepAliveTime?=?10;???//?允許線程空閑時間(單位:默認為秒),十秒后就把線程關閉
          ????private?static?final?int?queueCapacity?=?200;???//?緩沖隊列數(shù)
          ????private?static?final?String?threadNamePrefix?=?"it-is-threaddemo-";?//?線程池名前綴

          ????@Bean("threadPoolTaskExecutor")?//?bean的名稱,默認為首字母小寫的方法名
          ????public?ThreadPoolTaskExecutor?getDemoThread(){
          ????????ThreadPoolTaskExecutor?executor?=?new?ThreadPoolTaskExecutor();
          ????????executor.setCorePoolSize(corePoolSize);
          ????????executor.setMaxPoolSize(maxPoolSize);
          ????????executor.setQueueCapacity(keepAliveTime);
          ????????executor.setKeepAliveSeconds(queueCapacity);
          ????????executor.setThreadNamePrefix(threadNamePrefix);

          ????????//線程池拒絕任務的處理策略
          ????????executor.setRejectedExecutionHandler(new?ThreadPoolExecutor.CallerRunsPolicy());
          ????????//初始化
          ????????executor.initialize();

          ????????return?executor;
          ????}
          }

          然后我們可以很清晰地看到

          Springboot多定時器沖突的問題

          如上,也就解決了用多線程解決Springboot多定時器沖突的問題

          Springboot多定時器沖突的問題

          瀏覽 49
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  波多野吉衣无码HD | 一级性爱网站 | 国产成人高精内射 | 色情综合 | 青娱乐青青草视频 |