最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

使用@Scheduled 可以很容易實(shí)現(xiàn)定時(shí)任務(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ù)正在運(yùn)行的線程名稱:" + 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ù)正在運(yùn)行的線程名稱:" + thread.getName() + " 結(jié)束,start=" + simpleDateFormat.format(timeStamp) + ",end=" + simpleDateFormat.format(endStamp));
      System.out.println("====================");
    } catch (Exception e) {
      logger.error(e.getMessage());
    }
  }
}

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

cron2任務(wù)開始,start=2019-10-11 17:31:52, threadId=34, threadName=scheduling-1
cron2任務(wù)正在運(yùn)行的線程名稱: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ù)正在運(yùn)行的線程名稱: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ù)正在運(yùn)行的線程名稱:scheduling-1 結(jié)束,start=2019-10-11 17:32:22,end=2019-10-11 17:32:32

……

注:

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

原因:

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

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

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ù)正在運(yùn)行的線程名稱: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ù)正在運(yùn)行的線程名稱: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ù)正在運(yùn)行的線程名稱:myScheduling-2 結(jié)束,start=2019-10-11 17:34:49,end=2019-10-11 17:34:49

……

注:

多線程下,cron1和cron2不用互相等待了,但是同一個(gè)任務(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ù)正在運(yùn)行的線程名稱:" + 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ù)正在運(yùn)行的線程名稱:" + 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ù)正在運(yùn)行的線程名稱: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ù)正在運(yùn)行的線程名稱: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ù)正在運(yùn)行的線程名稱:task-4 結(jié)束,start=2019-10-11 17:40:03,end=2019-10-11 17:40:13
====================
cron1任務(wù)正在運(yùn)行的線程名稱:task-2 結(jié)束,start=2019-10-11 17:39:54,end=2019-10-11 17:39:54

說明: 

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

修改配置

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

重新運(yùn)行的輸出

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ù)正在運(yùn)行的線程名稱: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ù)正在運(yùn)行的線程名稱: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ù)正在運(yùn)行的線程名稱:mytask-4 結(jié)束,start=2019-10-11 17:44:10,end=2019-10-11 17:44:20
====================
cron1任務(wù)正在運(yùn)行的線程名稱:mytask-2 結(jié)束,start=2019-10-11 17:44:01,end=2019-10-11 17:44:01

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java IO流 文件的編碼實(shí)例代碼

    Java IO流 文件的編碼實(shí)例代碼

    本文通過實(shí)例代碼給大家介紹了java io流文件編碼的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2017-05-05
  • SpringBoot實(shí)現(xiàn)服務(wù)接入nacos注冊(cè)中心流程詳解

    SpringBoot實(shí)現(xiàn)服務(wù)接入nacos注冊(cè)中心流程詳解

    這篇文章主要介紹了SpringBoot實(shí)現(xiàn)服務(wù)接入nacos注冊(cè)中心流程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-01-01
  • Java計(jì)算兩個(gè)時(shí)間相差的秒數(shù)怎么算

    Java計(jì)算兩個(gè)時(shí)間相差的秒數(shù)怎么算

    這篇文章主要介紹了Java計(jì)算兩個(gè)時(shí)間相差的秒數(shù),通過實(shí)例代碼補(bǔ)充介紹了Java 獲取兩個(gè)時(shí)間的時(shí)間差(時(shí)、分、秒)問題,感興趣的朋友跟隨小編一起看看吧
    2024-03-03
  • Java五子棋單機(jī)版源碼分享

    Java五子棋單機(jī)版源碼分享

    這篇文章主要為大家分享了Java五子棋單機(jī)版源碼,JavaGUI編寫單機(jī)版五子棋,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • MybatisPlus自動(dòng)填充時(shí)間的配置類實(shí)現(xiàn)

    MybatisPlus自動(dòng)填充時(shí)間的配置類實(shí)現(xiàn)

    本文介紹了如何在MyBatis-Plus中實(shí)現(xiàn)自動(dòng)填充時(shí)間的功能,通過實(shí)現(xiàn)MetaObjectHandler接口,重寫insertFill()和updateFill()方法,分別在插入和更新時(shí)填充創(chuàng)建時(shí)間和更新時(shí)間,感興趣的可以了解一下
    2024-12-12
  • java連接zookeeper的實(shí)現(xiàn)示例

    java連接zookeeper的實(shí)現(xiàn)示例

    ZooKeeper官方提供了Java API,可以通過Java代碼來連接zookeeper服務(wù)進(jìn)行操作,本文就來介紹一下java連接zookeeper的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-11-11
  • 關(guān)于feign.codec.DecodeException異常的解決方案

    關(guān)于feign.codec.DecodeException異常的解決方案

    這篇文章主要介紹了關(guān)于feign.codec.DecodeException異常的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • idea使用帶provide修飾依賴導(dǎo)致ClassNotFound

    idea使用帶provide修飾依賴導(dǎo)致ClassNotFound

    程序打包到Linux上運(yùn)行時(shí),若Linux上也有這些依賴,為了在Linux上運(yùn)行時(shí)避免依賴沖突,可以使用provide修飾,本文主要介紹了idea使用帶provide修飾依賴導(dǎo)致ClassNotFound,下面就來介紹一下解決方法,感興趣的可以了解一下
    2024-01-01
  • Java System.exit()退出程序方式

    Java System.exit()退出程序方式

    這篇文章主要介紹了Java System.exit()退出程序方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Java實(shí)現(xiàn)限流接口的示例詳解

    Java實(shí)現(xiàn)限流接口的示例詳解

    限流是對(duì)某一時(shí)間窗口內(nèi)的請(qǐng)求數(shù)進(jìn)行限制,保持系統(tǒng)的可用性和穩(wěn)定性,防止因流量暴增而導(dǎo)致的系統(tǒng)運(yùn)行緩慢或宕機(jī),本文主要來和大家聊聊如何使用java實(shí)現(xiàn)限流接口,感興趣的可以了解下
    2023-12-12

最新評(píng)論

建德市| 衡阳县| 武安市| 九龙坡区| 婺源县| 射阳县| 监利县| 随州市| 紫阳县| 宜川县| 贺兰县| 廊坊市| 高阳县| 广河县| 乃东县| 凯里市| 张家界市| 浙江省| 平武县| 信丰县| 九龙坡区| 乐亭县| 昌平区| 遵义市| 沛县| 松溪县| 徐汇区| 中阳县| 定陶县| 巫溪县| 清流县| 阿克| 宽甸| 灵宝市| 清河县| 辽阳县| 木里| 金秀| 花垣县| 奇台县| 霍山县|