溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

SpringBoot中配置定時(shí)任務(wù)及線程池和多線程池執(zhí)行的方法是怎樣的

發(fā)布時(shí)間:2021-10-13 10:07:41 來源:億速云 閱讀:112 作者:柒染 欄目:編程語言

SpringBoot中配置定時(shí)任務(wù)及線程池和多線程池執(zhí)行的方法是怎樣的,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

配置基礎(chǔ)的定時(shí)任務(wù)

最基本的配置方法,而且這樣配置定時(shí)任務(wù)是單線程串行執(zhí)行的,也就是說每次只能有一個(gè)定時(shí)任務(wù)可以執(zhí)行,可以試著聲明兩個(gè)方法,在方法內(nèi)寫一個(gè)死循環(huán),會(huì)發(fā)現(xiàn)一直卡在一個(gè)任務(wù)上不動(dòng),另一個(gè)也沒有執(zhí)行。

1、啟動(dòng)類

添加@EnableScheduling開啟對(duì)定時(shí)任務(wù)的支持

@EnableScheduling@SpringBootApplicationpublic class TestScheduledApplication extends SpringBootServletInitializer { @Override  protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {  return builder.sources(this.getClass()); } public static void main(String[] args) {  new SpringApplicationBuilder(TestScheduledApplication.class).web(true).run(args); }}

2、配置執(zhí)行定時(shí)任務(wù)的類

添加@Component掃描本類,在方法上添加@Scheduled注解聲明定時(shí)任務(wù),配置時(shí)間周期

@Componentpublic class TestTask1 { private static final Logger logger = LogManager.getLogger(); // 間隔1秒執(zhí)行一次 @Scheduled(cron = "0/1 * * * * ?") public void method1() {  logger.info("——————————method1 start——————————");  logger.info("——————————method1 end——————————"); }}

配置線程池執(zhí)行定時(shí)任務(wù)

因?yàn)橛袝r(shí)候需要執(zhí)行的定時(shí)任務(wù)會(huì)很多,如果是串行執(zhí)行會(huì)帶來一些問題,比如一個(gè)很耗時(shí)的任務(wù)阻塞住了,一些需要短周期循環(huán)執(zhí)行的任務(wù)也會(huì)卡住,所以可以配置一個(gè)線程池來并行執(zhí)行定時(shí)任務(wù)

1、配置線程池

添加@EnableAsync開啟對(duì)異步的支持

@Configuration@EnableAsyncpublic class ExecutorConfig {  @Bean public Executor executor1() {  ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();  executor.setThreadNamePrefix("test-schedule2-");  executor.setMaxPoolSize(20);  executor.setCorePoolSize(15);  executor.setQueueCapacity(0);  executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());  return executor; }}

2、配置定時(shí)任務(wù)異步執(zhí)行

添加@Async注解,表示該定時(shí)任務(wù)是異步執(zhí)行的,因?yàn)樯厦婢€程池配置了名字,所以可以看到打印的日志是該線程池中的線程在執(zhí)行任務(wù),如果沒有配置線程池的話會(huì)默認(rèn)使用SimpleAsyncTaskExecutor,這個(gè)異步執(zhí)行器每次都會(huì)開啟一個(gè)子線程執(zhí)行,性能消耗比較大,所以最好是自己配置線程池

@Async@Scheduled(cron = "0/1 * * * * ?")public void method1() { logger.info("——————————method1 start——————————"); logger.info("——————————method1 end——————————");}

配置多個(gè)線程池分別執(zhí)行不同的定時(shí)任務(wù)

因?yàn)橛行┒〞r(shí)任務(wù)是比較重要的,有些則是不太重要的,想把定時(shí)任務(wù)分別放到不同的線程池中,也是可以實(shí)現(xiàn)的。

1、配置多個(gè)線程池

分別配置兩個(gè)線程池

@Configuration@EnableAsyncpublic class ExecutorConfig1 { @Bean public Executor executor1() {  ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();  executor.setThreadNamePrefix("test-schedule1-");  executor.setMaxPoolSize(20);  executor.setCorePoolSize(15);  executor.setQueueCapacity(0);  executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());  return executor; }  @Bean public Executor executor2() {  ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();  executor.setThreadNamePrefix("test-schedule2-");  executor.setMaxPoolSize(20);  executor.setCorePoolSize(15);  executor.setQueueCapacity(0);  executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());  return executor;  }}

2、定時(shí)任務(wù)顯示指定調(diào)用線程池

因?yàn)樯厦嬖谂渲妙惱锩娉跏蓟藘蓚€(gè)線程池,所以會(huì)有兩個(gè)線程池分別叫executor1和executor1被生成放到容器中,因?yàn)锧Bean注解生成的對(duì)象默認(rèn)就是和方法名相同的名字,而@Async注解是可以指定使用哪個(gè)線程池的。這樣就可以在不同的線程池中執(zhí)行不同的定時(shí)任務(wù)了

// 間隔1秒執(zhí)行一次@Async("executor1")@Scheduled(cron = "0/1 * * * * ?")public void method1() { logger.info("——————————method1 start——————————"); logger.info("——————————method1 end——————————");}// 間隔1秒執(zhí)行一次@Scheduled(cron = "0/1 * * * * ?")@Async("executor2")public void method2() { logger.info("——————————method2 start——————————"); logger.info("——————————method2 end——————————");}

注意:

  1. 沒有配置自己的線程池時(shí),會(huì)默認(rèn)使用SimpleAsyncTaskExecutor。  如果項(xiàng)目中只配置了一個(gè)線程池,那么不需要顯示指定使用這個(gè)線程池,spring也會(huì)自動(dòng)使用用戶配置的線程池,但是如果配置了多個(gè)就必須要顯示指定,否則還是會(huì)使用默認(rèn)的。  如果想要指定使用哪個(gè)線程池,可以使用@Async("executor2")顯示指定。

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI