您好,登錄后才能下訂單哦!
本篇文章為大家展示了Springboot中怎么利用@Scheduled實現定時任務,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
使用@Scheduled 可以很容易實現定時任務
spring boot的版本 2.1.6.RELEASE
package com.abc.demo.common;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;import java.text.SimpleDateFormat;import java.util.concurrent.TimeUnit;@EnableScheduling@Componentpublic class ScheduleSetting { private final Logger logger = LoggerFactory.getLogger(Tasks.class); @Scheduled(fixedRate = 10000, initialDelay = 2000) public void scheduleRead() { try { long timeStamp = System.currentTimeMillis(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Thread thread = Thread.currentThread(); System.out.println("cron1任務開始,start=" + simpleDateFormat.format(timeStamp) + ", threadId=" + thread.getId() + ", threadName=" + thread.getName()); long endStamp = System.currentTimeMillis(); try { TimeUnit.SECONDS.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("cron1任務正在運行的線程名稱:" + thread.getName() + " 結束,start=" + simpleDateFormat.format(timeStamp) + ",end=" + simpleDateFormat.format(endStamp)); System.out.println("++++++++++++++++++++++++"); } catch (Exception e) { logger.error(e.getMessage()); } } @Scheduled(fixedRate = 5000, initialDelay = 1000) public void scheduleConvert() { try { long timeStamp = System.currentTimeMillis(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Thread thread = Thread.currentThread(); System.out.println("cron2任務開始,start=" + simpleDateFormat.format(timeStamp) + ", threadId=" + thread.getId() + ", threadName=" + thread.getName()); try { TimeUnit.SECONDS.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } long endStamp = System.currentTimeMillis(); System.out.println("cron2任務正在運行的線程名稱:" + thread.getName() + " 結束,start=" + simpleDateFormat.format(timeStamp) + ",end=" + simpleDateFormat.format(endStamp)); System.out.println("===================="); } catch (Exception e) { logger.error(e.getMessage()); } }}
運行輸出內容為
cron2任務開始,start=2019-10-11 17:31:52, threadId=34, threadName=scheduling-1cron2任務正在運行的線程名稱:scheduling-1 結束,start=2019-10-11 17:31:52,end=2019-10-11 17:32:02====================cron1任務開始,start=2019-10-11 17:32:02, threadId=34, threadName=scheduling-1cron1任務正在運行的線程名稱:scheduling-1 結束,start=2019-10-11 17:32:02,end=2019-10-11 17:32:02++++++++++++++++++++++++cron2任務開始,start=2019-10-11 17:32:22, threadId=34, threadName=scheduling-1cron2任務正在運行的線程名稱:scheduling-1 結束,start=2019-10-11 17:32:22,end=2019-10-11 17:32:32……
注:
cron2執行完后才會執行cron1
原因:
spring默認是以單線程執行任務調度
spring的定時任務默認最大運行線程數為1,多個任務執行起來時間會有問題
1.配置線程池
在配置文件application.properties中添加
# 線程池大小spring.task.scheduling.pool.size=5# 線程名前綴spring.task.scheduling.thread-name-prefix=myScheduling-
輸出內容變為
cron2任務開始,start=2019-10-11 17:34:48, threadId=34, threadName=myScheduling-1cron1任務開始,start=2019-10-11 17:34:49, threadId=35, threadName=myScheduling-2cron2任務正在運行的線程名稱:myScheduling-1 結束,start=2019-10-11 17:34:48,end=2019-10-11 17:34:58====================cron2任務開始,start=2019-10-11 17:34:58, threadId=34, threadName=myScheduling-1cron2任務正在運行的線程名稱:myScheduling-1 結束,start=2019-10-11 17:34:58,end=2019-10-11 17:35:08====================cron2任務開始,start=2019-10-11 17:35:08, threadId=57, threadName=myScheduling-3cron1任務正在運行的線程名稱:myScheduling-2 結束,start=2019-10-11 17:34:49,end=2019-10-11 17:34:49……
注:
多線程下,cron1和cron2不用互相等待了,但是同一個任務還是需要等待的
2.并發
修改代碼
package com.abc.demo.common;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.scheduling.annotation.Async;import org.springframework.scheduling.annotation.EnableAsync;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;import java.text.SimpleDateFormat;import java.util.concurrent.TimeUnit;@EnableScheduling@Component@EnableAsyncpublic class ScheduleSetting { private final Logger logger = LoggerFactory.getLogger(Tasks.class); @Async @Scheduled(fixedRate = 10000, initialDelay = 2000) public void scheduleRead() { try { long timeStamp = System.currentTimeMillis(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Thread thread = Thread.currentThread(); System.out.println("cron1任務開始,start=" + simpleDateFormat.format(timeStamp) + ", threadId=" + thread.getId() + ", threadName=" + thread.getName()); long endStamp = System.currentTimeMillis(); try { TimeUnit.SECONDS.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("cron1任務正在運行的線程名稱:" + thread.getName() + " 結束,start=" + simpleDateFormat.format(timeStamp) + ",end=" + simpleDateFormat.format(endStamp)); System.out.println("++++++++++++++++++++++++"); } catch (Exception e) { logger.error(e.getMessage()); } } @Async @Scheduled(fixedRate = 5000, initialDelay = 1000) public void scheduleConvert() { try { long timeStamp = System.currentTimeMillis(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Thread thread = Thread.currentThread(); System.out.println("cron2任務開始,start=" + simpleDateFormat.format(timeStamp) + ", threadId=" + thread.getId() + ", threadName=" + thread.getName()); try { TimeUnit.SECONDS.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } long endStamp = System.currentTimeMillis(); System.out.println("cron2任務正在運行的線程名稱:" + thread.getName() + " 結束,start=" + simpleDateFormat.format(timeStamp) + ",end=" + simpleDateFormat.format(endStamp)); System.out.println("===================="); } catch (Exception e) { logger.error(e.getMessage()); } }}
輸出的內容
cron2任務開始,start=2019-10-11 17:39:53, threadId=57, threadName=task-1cron1任務開始,start=2019-10-11 17:39:54, threadId=59, threadName=task-2cron2任務開始,start=2019-10-11 17:39:58, threadId=61, threadName=task-3cron2任務開始,start=2019-10-11 17:40:03, threadId=63, threadName=task-4cron2任務正在運行的線程名稱:task-1 結束,start=2019-10-11 17:39:53,end=2019-10-11 17:40:03====================cron1任務開始,start=2019-10-11 17:40:04, threadId=64, threadName=task-5cron2任務開始,start=2019-10-11 17:40:08, threadId=65, threadName=task-6cron2任務正在運行的線程名稱:task-3 結束,start=2019-10-11 17:39:58,end=2019-10-11 17:40:08====================cron2任務開始,start=2019-10-11 17:40:13, threadId=66, threadName=task-7cron2任務正在運行的線程名稱:task-4 結束,start=2019-10-11 17:40:03,end=2019-10-11 17:40:13====================cron1任務正在運行的線程名稱:task-2 結束,start=2019-10-11 17:39:54,end=2019-10-11 17:39:54
說明:
@EnableAsync開啟多線程 @Async標記其為一個異步任務 每個定時任務都是在通過不同的線程來處理,線程名的前綴成了task- 線程默認為10個
修改配置
spring.task.execution.thread-name-prefix=mytask-spring.task.execution.pool.core-size=5
重新運行的輸出
cron2任務開始,start=2019-10-11 17:44:00, threadId=56, threadName=mytask-1cron1任務開始,start=2019-10-11 17:44:01, threadId=57, threadName=mytask-2cron2任務開始,start=2019-10-11 17:44:05, threadId=58, threadName=mytask-3cron2任務開始,start=2019-10-11 17:44:10, threadId=59, threadName=mytask-4cron2任務正在運行的線程名稱:mytask-1 結束,start=2019-10-11 17:44:00,end=2019-10-11 17:44:10====================cron1任務開始,start=2019-10-11 17:44:11, threadId=60, threadName=mytask-5cron2任務正在運行的線程名稱:mytask-3 結束,start=2019-10-11 17:44:05,end=2019-10-11 17:44:15====================cron2任務開始,start=2019-10-11 17:44:15, threadId=58, threadName=mytask-3cron2任務開始,start=2019-10-11 17:44:20, threadId=56, threadName=mytask-1cron2任務正在運行的線程名稱:mytask-4 結束,start=2019-10-11 17:44:10,end=2019-10-11 17:44:20====================cron1任務正在運行的線程名稱:mytask-2 結束,start=2019-10-11 17:44:01,end=2019-10-11 17:44:01
上述內容就是Springboot中怎么利用@Scheduled實現定時任務,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。