springBoot?@Scheduled實現(xiàn)多個任務(wù)同時開始執(zhí)行
@Scheduled多個任務(wù)同時開始執(zhí)行
只需在springBoot啟動類上添加
如下代碼即可:
@Bean public TaskScheduler taskScheduler() { ThreadPoolTaskScheduler taskExecutor = new ThreadPoolTaskScheduler(); taskExecutor.setPoolSize(50); return taskExecutor; }
@Scheduled多定時任務(wù),重疊執(zhí)行
@Scheduled如果有兩個定時任務(wù)
定時任務(wù)重復(fù)時,只有一個可以執(zhí)行。
如下
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.time.LocalDateTime; @Component public class MyScheduled { @Scheduled(cron = "0/5 * * * * ?") public void execute1(){ String curName = Thread.currentThread().getName() ; System.out.println("當(dāng)前時間:"+LocalDateTime.now()+" 任務(wù)execute1對應(yīng)的線程名: "+curName); try { Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } } @Scheduled(cron = "0/5 * * * * ?") public void execute2(){ String curName = Thread.currentThread().getName() ; System.out.println("當(dāng)前時間:"+LocalDateTime.now()+" 任務(wù)execute2對應(yīng)的線程名: "+curName); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }
通過執(zhí)行可以看到,打印線程名稱為同一個。即如果不手動指定線程池,則默認(rèn)啟動單線程,進(jìn)行執(zhí)行定時任務(wù)。
如果想要多個定時任務(wù)重疊執(zhí)行
需要手動指定線程池,如下
import org.springframework.context.annotation.Bean; import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.stereotype.Component; import java.time.LocalDateTime; @Component @EnableScheduling public class MyScheduled { @Bean public TaskScheduler taskScheduler() { ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); taskScheduler.setPoolSize(50); return taskScheduler; } @Scheduled(cron = "0/5 * * * * ?") public void execute1(){ String curName = Thread.currentThread().getName() ; System.out.println("當(dāng)前時間:"+LocalDateTime.now()+" 任務(wù)execute1對應(yīng)的線程名: "+curName); try { Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } } @Scheduled(cron = "0/5 * * * * ?") public void execute2(){ String curName = Thread.currentThread().getName() ; System.out.println("當(dāng)前時間:"+LocalDateTime.now()+" 任務(wù)execute2對應(yīng)的線程名: "+curName); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }
此時,多個定時任務(wù),是不通的線程執(zhí)行,同時,定時任務(wù)可以重疊執(zhí)行。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot+mail 輕松實現(xiàn)各類郵件自動推送
在實際的項目開發(fā)過程中,經(jīng)常需要用到郵件通知功能,例如,通過郵箱注冊,郵箱找回密碼,郵箱推送報表等等,實際的應(yīng)用場景非常的多,今天通過這篇文章,我們一起來學(xué)習(xí)如何在 Spring Boot 中快速實現(xiàn)一個自動發(fā)送郵件的功能2024-07-07防止未登錄用戶操作—基于struts2攔截器的簡單實現(xiàn)
下面小編就為大家?guī)硪黄乐刮吹卿浻脩舨僮鳌趕truts2攔截器的簡單實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10Mybatis?plus多租戶方案的實戰(zhàn)踩坑記錄
MybaitsPlus多租戶處理器是一個對于多租戶問題的解決方案,下面這篇文章主要給大家介紹了關(guān)于Mybatis?plus多租戶方案踩坑的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12SpringBoot?模板模式實現(xiàn)優(yōu)惠券邏輯的示例代碼
這篇文章主要介紹了SpringBoot?模板模式實現(xiàn)優(yōu)惠券邏輯,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08springboot無法跳轉(zhuǎn)頁面的問題解決方案
這篇文章主要介紹了springboot無法跳轉(zhuǎn)頁面的問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09springboot集成redis并使用redis生成全局唯一索引ID
本文主要介紹了springboot集成redis并使用redis生成全局唯一索引ID,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03