欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring boot如何通過@Scheduled實現(xiàn)定時任務(wù)及多線程配置

 更新時間:2019年12月02日 10:09:09   作者:慕塵  
這篇文章主要介紹了Spring boot如何通過@Scheduled實現(xiàn)定時任務(wù)及多線程配置,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

這篇文章主要介紹了Spring boot如何通過@Scheduled實現(xiàn)定時任務(wù)及多線程配置,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

使用@Scheduled 可以很容易實現(xiàn)定時任務(wù)

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
@Component
public 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任務(wù)開始,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任務(wù)正在運行的線程名稱:" + thread.getName() + " 結(jié)束,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任務(wù)開始,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任務(wù)正在運行的線程名稱:" + thread.getName() + " 結(jié)束,start=" + simpleDateFormat.format(timeStamp) + ",end=" + simpleDateFormat.format(endStamp));
      System.out.println("====================");
    } catch (Exception e) {
      logger.error(e.getMessage());
    }
  }
}

運行輸出內(nèi)容為

cron2任務(wù)開始,start=2019-10-11 17:31:52, threadId=34, threadName=scheduling-1
cron2任務(wù)正在運行的線程名稱:scheduling-1 結(jié)束,start=2019-10-11 17:31:52,end=2019-10-11 17:32:02
====================
cron1任務(wù)開始,start=2019-10-11 17:32:02, threadId=34, threadName=scheduling-1
cron1任務(wù)正在運行的線程名稱:scheduling-1 結(jié)束,start=2019-10-11 17:32:02,end=2019-10-11 17:32:02
++++++++++++++++++++++++
cron2任務(wù)開始,start=2019-10-11 17:32:22, threadId=34, threadName=scheduling-1
cron2任務(wù)正在運行的線程名稱:scheduling-1 結(jié)束,start=2019-10-11 17:32:22,end=2019-10-11 17:32:32

……

注:

  cron2執(zhí)行完后才會執(zhí)行cron1

原因:

  spring默認是以單線程執(zhí)行任務(wù)調(diào)度

  spring的定時任務(wù)默認最大運行線程數(shù)為1,多個任務(wù)執(zhí)行起來時間會有問題

1.配置線程池

在配置文件application.properties中添加

# 線程池大小
spring.task.scheduling.pool.size=5
# 線程名前綴
spring.task.scheduling.thread-name-prefix=myScheduling-

輸出內(nèi)容變?yōu)?/p>

cron2任務(wù)開始,start=2019-10-11 17:34:48, threadId=34, threadName=myScheduling-1
cron1任務(wù)開始,start=2019-10-11 17:34:49, threadId=35, threadName=myScheduling-2
cron2任務(wù)正在運行的線程名稱:myScheduling-1 結(jié)束,start=2019-10-11 17:34:48,end=2019-10-11 17:34:58
====================
cron2任務(wù)開始,start=2019-10-11 17:34:58, threadId=34, threadName=myScheduling-1
cron2任務(wù)正在運行的線程名稱:myScheduling-1 結(jié)束,start=2019-10-11 17:34:58,end=2019-10-11 17:35:08
====================
cron2任務(wù)開始,start=2019-10-11 17:35:08, threadId=57, threadName=myScheduling-3
cron1任務(wù)正在運行的線程名稱:myScheduling-2 結(jié)束,start=2019-10-11 17:34:49,end=2019-10-11 17:34:49

……

注:

多線程下,cron1和cron2不用互相等待了,但是同一個任務(wù)還是需要等待的

2.并發(fā)

修改代碼

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
@EnableAsync
public 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任務(wù)開始,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任務(wù)正在運行的線程名稱:" + thread.getName() + " 結(jié)束,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任務(wù)開始,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任務(wù)正在運行的線程名稱:" + thread.getName() + " 結(jié)束,start=" + simpleDateFormat.format(timeStamp) + ",end=" + simpleDateFormat.format(endStamp));
      System.out.println("====================");
    } catch (Exception e) {
      logger.error(e.getMessage());
    }
  }
}

輸出的內(nèi)容

cron2任務(wù)開始,start=2019-10-11 17:39:53, threadId=57, threadName=task-1
cron1任務(wù)開始,start=2019-10-11 17:39:54, threadId=59, threadName=task-2
cron2任務(wù)開始,start=2019-10-11 17:39:58, threadId=61, threadName=task-3
cron2任務(wù)開始,start=2019-10-11 17:40:03, threadId=63, threadName=task-4
cron2任務(wù)正在運行的線程名稱:task-1 結(jié)束,start=2019-10-11 17:39:53,end=2019-10-11 17:40:03
====================
cron1任務(wù)開始,start=2019-10-11 17:40:04, threadId=64, threadName=task-5
cron2任務(wù)開始,start=2019-10-11 17:40:08, threadId=65, threadName=task-6
cron2任務(wù)正在運行的線程名稱:task-3 結(jié)束,start=2019-10-11 17:39:58,end=2019-10-11 17:40:08
====================
cron2任務(wù)開始,start=2019-10-11 17:40:13, threadId=66, threadName=task-7
cron2任務(wù)正在運行的線程名稱:task-4 結(jié)束,start=2019-10-11 17:40:03,end=2019-10-11 17:40:13
====================
cron1任務(wù)正在運行的線程名稱:task-2 結(jié)束,start=2019-10-11 17:39:54,end=2019-10-11 17:39:54

說明: 

  •   @EnableAsync開啟多線程
  •   @Async標記其為一個異步任務(wù)
  •   每個定時任務(wù)都是在通過不同的線程來處理,線程名的前綴成了task-
  •   線程默認為10個

修改配置

spring.task.execution.thread-name-prefix=mytask-
spring.task.execution.pool.core-size=5

重新運行的輸出

cron2任務(wù)開始,start=2019-10-11 17:44:00, threadId=56, threadName=mytask-1
cron1任務(wù)開始,start=2019-10-11 17:44:01, threadId=57, threadName=mytask-2
cron2任務(wù)開始,start=2019-10-11 17:44:05, threadId=58, threadName=mytask-3
cron2任務(wù)開始,start=2019-10-11 17:44:10, threadId=59, threadName=mytask-4
cron2任務(wù)正在運行的線程名稱:mytask-1 結(jié)束,start=2019-10-11 17:44:00,end=2019-10-11 17:44:10
====================
cron1任務(wù)開始,start=2019-10-11 17:44:11, threadId=60, threadName=mytask-5
cron2任務(wù)正在運行的線程名稱:mytask-3 結(jié)束,start=2019-10-11 17:44:05,end=2019-10-11 17:44:15
====================
cron2任務(wù)開始,start=2019-10-11 17:44:15, threadId=58, threadName=mytask-3
cron2任務(wù)開始,start=2019-10-11 17:44:20, threadId=56, threadName=mytask-1
cron2任務(wù)正在運行的線程名稱:mytask-4 結(jié)束,start=2019-10-11 17:44:10,end=2019-10-11 17:44:20
====================
cron1任務(wù)正在運行的線程名稱:mytask-2 結(jié)束,start=2019-10-11 17:44:01,end=2019-10-11 17:44:01

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java?List集合去除null的4種方法

    java?List集合去除null的4種方法

    這篇文章主要給大家介紹了java?List集合去除null的3種方法,文中通過代碼示例將每種方法都介紹的非常詳細,對大家學習或者使用Java具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-09-09
  • Mybatis-Plus3.2.0 MetaObjectHandler 無法進行公共字段全局填充

    Mybatis-Plus3.2.0 MetaObjectHandler 無法進行公共字段全局填充

    這篇文章主要介紹了Mybatis-Plus3.2.0 MetaObjectHandler 無法進行公共字段全局填充,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-11-11
  • Java基本語法小白入門級

    Java基本語法小白入門級

    Java基本語法就是指java中的規(guī)則,也是一種語言規(guī)則,規(guī)范,同時也能讓您在后面的學習中避免不必要的一些錯誤和麻煩,是您學好java必修的第一門課程
    2023-05-05
  • Java JDK17沒有源碼的問題及解決

    Java JDK17沒有源碼的問題及解決

    這篇文章主要介紹了Java JDK17沒有源碼的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Java中終止線程的三種方法

    Java中終止線程的三種方法

    這篇文章主要為大家詳細介紹了Java中終止線程的三種方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • SpringBoot之Helloword 快速搭建一個web項目(圖文)

    SpringBoot之Helloword 快速搭建一個web項目(圖文)

    這篇文章主要介紹了SpringBoot之Helloword 快速搭建一個web項目(圖文),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • IDEA進程已結(jié)束,退出代碼-1073741819 (0xC0000005)的bug

    IDEA進程已結(jié)束,退出代碼-1073741819 (0xC0000005)的bug

    這篇文章主要介紹了IDEA進程已結(jié)束,退出代碼-1073741819 (0xC0000005)的bug,本文通過實例代碼圖文的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04
  • Java中進程、協(xié)程與線程的區(qū)別詳解

    Java中進程、協(xié)程與線程的區(qū)別詳解

    這篇文章主要介紹了Java中進程,線程,協(xié)程的概念、區(qū)別以及使用場景的選擇,早期的操作系統(tǒng)每個程序就是一個進程,知道一個程序運行完,才能進行下一個進程,就是"單進程時代",一切的程序只能串行發(fā)生,需要的朋友可以參考下
    2023-08-08
  • hutool實戰(zhàn):IoUtil 流操作工具類(將內(nèi)容寫到流中)

    hutool實戰(zhàn):IoUtil 流操作工具類(將內(nèi)容寫到流中)

    這篇文章主要介紹了Go語言的io.ioutil標準庫使用,是Golang入門學習中的基礎(chǔ)知識,需要的朋友可以參考下,如果能給你帶來幫助,請多多關(guān)注腳本之家的其他內(nèi)容
    2021-06-06
  • Java對敏感數(shù)據(jù)進行加密的方法詳解

    Java對敏感數(shù)據(jù)進行加密的方法詳解

    敏感數(shù)據(jù)的加密是數(shù)據(jù)安全的重要方面,尤其是對于手機號和身份證號這類個人信息,本文主要為大家介紹了Java對敏感數(shù)據(jù)進行加密的相關(guān)方法,希望對大家有所幫助
    2024-03-03

最新評論