<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 線程池配置

          共 3045字,需瀏覽 7分鐘

           ·

          2020-11-16 23:04

          點擊上方藍(lán)色字體,選擇“標(biāo)星公眾號”

          優(yōu)質(zhì)文章,第一時間送達(dá)

          ? 作者?|??明天,你好啊

          來源 |? urlify.cn/EjE7be

          66套java從入門到精通實戰(zhàn)課程分享

          springboot 集成異步線程池

          目的:通過實現(xiàn)AsyncConfigurer自定義線程池,包含異常處理。?實現(xiàn)AsyncConfigurer接口對異常線程池更加細(xì)粒度的控制

          /**
          ?*?@Description:?線程池配置
          ?*?@Author:?mingtian
          ?*?@CreateDate:?2020/11/12?15:57
          ?*?@Version:?1.0
          ?*/
          @Configuration
          @EnableAsync
          public?class?ThreadPoolConfig?implements?AsyncConfigurer?{

          ????/**
          ?????*?打印日志
          ?????*/
          ????private?Logger?logger?=?LoggerFactory.getLogger(getClass());

          ????/**
          ?????*?cpu?核心數(shù)量
          ?????*/
          ????public?static?final?int?cpuNum?=?Runtime.getRuntime().availableProcessors();

          ????/**
          ?????*?線程池配置
          ?????*
          ?????*?@return
          ?????*/
          ????@Bean("taskExecutor")
          ????@Override
          ????public?Executor?getAsyncExecutor()?{
          ????????ThreadPoolTaskExecutor?taskExecutor?=?new?ThreadPoolTaskExecutor();
          ????????//?配置核心線程池數(shù)量
          ????????taskExecutor.setCorePoolSize(cpuNum);
          ????????//?配置最大線程池數(shù)量
          ????????taskExecutor.setMaxPoolSize(cpuNum?*?2);
          ????????///?線程池所使用的緩沖隊列
          ????????taskExecutor.setQueueCapacity(2);
          ????????//?等待時間?(默認(rèn)為0,此時立即停止),并沒等待xx秒后強(qiáng)制停止
          ????????taskExecutor.setAwaitTerminationSeconds(60);
          ????????//?空閑線程存活時間
          ????????taskExecutor.setKeepAliveSeconds(60);
          ????????//?等待任務(wù)在關(guān)機(jī)時完成--表明等待所有線程執(zhí)行完
          ????????taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
          ????????//?線程池名稱前綴
          ????????taskExecutor.setThreadNamePrefix("thread-pool-");
          ????????//?線程池拒絕策略
          ????????taskExecutor.setRejectedExecutionHandler(new?ThreadPoolExecutor.DiscardOldestPolicy());
          ????????//?線程池初始化
          ????????taskExecutor.initialize();
          ????????logger.info("線程池初始化......");
          ????????return?taskExecutor;
          ????}

          ????/**
          ?????*?重寫捕獲異常類
          ?????*
          ?????*?@return
          ?????*/
          ????@Override
          ????public?AsyncUncaughtExceptionHandler?getAsyncUncaughtExceptionHandler()?{
          ????????return?new?MyAsyncExceptionHandler();
          ????}

          ????/**
          ?????*?自定義異常處理類
          ?????*/
          ????class?MyAsyncExceptionHandler?implements?AsyncUncaughtExceptionHandler?{
          ????????//手動處理捕獲的異常
          ????????@Override
          ????????public?void?handleUncaughtException(Throwable?throwable,?Method?method,?Object...?obj)?{
          ????????????logger.error("ExceptionMessage:{}",?throwable.getMessage());
          ????????????logger.error("MethodName:{}",?method.getName());
          ????????????for?(Object?param?:?obj)?{
          ????????????????logger.error("Parameter:{}",?param);
          ????????????}
          ????????}
          ????}
          }


          模擬發(fā)送消息業(yè)務(wù)層

          /**
          ?*?@Description:?模擬異步發(fā)送消息方法
          ?*?@Author:?mingtian
          ?*?@CreateDate:?2020/11/12?16:29
          ?*?@Version:?1.0
          ?*/
          @Component
          public?class?SendMessageService?{
          ????/**
          ?????*?打印日志
          ?????*/
          ????private?Logger?logger?=?LoggerFactory.getLogger(getClass());

          ????@Async
          ????public?void?sendMessage()?{
          ????????logger.info("發(fā)送消息");
          ????????System.out.println("子線程名稱:"?+?Thread.currentThread().getName());
          ????}
          }

          測試類

          /**
          ?*?@Description:?測試類
          ?*?@Author:?mingtian
          ?*?@CreateDate:?2020/11/12?16:30
          ?*?@Version:?1.0
          ?*/
          @RunWith(SpringRunner.class)
          @SpringBootTest
          public?class?Test?{
          ????@Autowired
          ????private?SendMessageService?messageService;

          [email protected]
          ????public?void?testAsync()?{
          ????????System.out.println("主線程名稱:"?+?Thread.currentThread().getName());
          ????????for?(int?i?=?0;?i?????????????messageService.sendMessage();
          ????????}

          ????}
          }

          控制臺打印結(jié)果:

          主線程名稱:main
          2020-11-12?16:47:48.985??INFO?16728?---?[??thread-pool-6]?c.example.threadpool.SendMessageService??:?異步發(fā)送發(fā)送消息
          2020-11-12?16:47:48.985??INFO?16728?---?[??thread-pool-5]?c.example.threadpool.SendMessageService??:?異步發(fā)送發(fā)送消息
          2020-11-12?16:47:48.985??INFO?16728?---?[??thread-pool-6]?c.example.threadpool.SendMessageService??:?異步發(fā)送發(fā)送消息
          2020-11-12?16:47:48.985??INFO?16728?---?[??thread-pool-7]?c.example.threadpool.SendMessageService??:?異步發(fā)送發(fā)送消息
          2020-11-12?16:47:48.985??INFO?16728?---?[??thread-pool-5]?c.example.threadpool.SendMessageService??:?異步發(fā)送發(fā)送消息
          2020-11-12?16:47:48.985??INFO?16728?---?[??thread-pool-8]?c.example.threadpool.SendMessageService??:?異步發(fā)送發(fā)送消息
          2020-11-12?16:47:48.985??INFO?16728?---?[??thread-pool-4]?c.example.threadpool.SendMessageService??:?異步發(fā)送發(fā)送消息
          2020-11-12?16:47:48.985??INFO?16728?---?[??thread-pool-1]?c.example.threadpool.SendMessageService??:?異步發(fā)送發(fā)送消息
          2020-11-12?16:47:48.985??INFO?16728?---?[??thread-pool-3]?c.example.threadpool.SendMessageService??:?異步發(fā)送發(fā)送消息
          2020-11-12?16:47:48.985??INFO?16728?---?[??thread-pool-2]?c.example.threadpool.SendMessageService??:?異步發(fā)送發(fā)送消息

          由以上的結(jié)果得出配置的線程池是有效的。




          粉絲福利:實戰(zhàn)springboot+CAS單點登錄系統(tǒng)視頻教程免費領(lǐng)取

          ???

          ?長按上方微信二維碼?2 秒
          即可獲取資料



          感謝點贊支持下哈?

          瀏覽 48
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

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

          手機(jī)掃一掃分享

          分享
          舉報
          <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>
                  爱爱网址免费看 | 《精品 模特私拍秘 泄密》学院派 | 日韩无码乱伦小说 | 青青操在线播放 | 亚洲日韩在线天堂 |