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

SpringBoot異步任務(wù)使用方法詳解

 更新時(shí)間:2020年03月13日 12:29:17   作者:玉天恒  
這篇文章主要介紹了SpringBoot異步任務(wù)使用方法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

步驟,如圖所示:

1.添加異步任務(wù)業(yè)務(wù)類

package top.ytheng.demo.task;

import java.util.concurrent.Future;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

//異步任務(wù)業(yè)務(wù)類
@Component
//標(biāo)記此類是異步類,也可在方法中標(biāo)記
//不加,則類里面的方法為同步執(zhí)行
@Async
public class AsyncTask {

  public void task1() throws InterruptedException {
    long begin = System.currentTimeMillis();
    Thread.sleep(1000);
    long end = System.currentTimeMillis();
    System.out.println("任務(wù)1耗時(shí):" + (end - begin));
  }
  
  public void task2() throws InterruptedException {
    long begin = System.currentTimeMillis();
    Thread.sleep(2000);
    long end = System.currentTimeMillis();
    System.out.println("任務(wù)2耗時(shí):" + (end - begin));
  }
  
  public void task3() throws InterruptedException {
    long begin = System.currentTimeMillis();
    Thread.sleep(3000);
    long end = System.currentTimeMillis();
    System.out.println("任務(wù)3耗時(shí):" + (end - begin));
  }
  
  //測(cè)試拿到返回結(jié)果
  public Future<String> task4() throws InterruptedException {
    long begin = System.currentTimeMillis();
    Thread.sleep(1000);
    long end = System.currentTimeMillis();
    System.out.println("任務(wù)4耗時(shí):" + (end - begin));
    return new AsyncResult<String>("任務(wù)4");
  }
  
  public Future<String> task5() throws InterruptedException {
    long begin = System.currentTimeMillis();
    Thread.sleep(2000);
    long end = System.currentTimeMillis();
    System.out.println("任務(wù)5耗時(shí):" + (end - begin));
    return new AsyncResult<String>("任務(wù)5");
  }
  
  public Future<String> task6() throws InterruptedException {
    long begin = System.currentTimeMillis();
    Thread.sleep(3000);
    long end = System.currentTimeMillis();
    System.out.println("任務(wù)6耗時(shí):" + (end - begin));
    return new AsyncResult<String>("任務(wù)6");
  }
}

2.添加測(cè)試控制器

package top.ytheng.demo.controller;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import top.ytheng.demo.task.AsyncTask;

@RestController
@RequestMapping("api/v1/async")
public class TaskController {

  @Autowired
  private AsyncTask asyncTask;
  
  @GetMapping("/test")
  public Object test() throws InterruptedException, ExecutionException {
    long begin = System.currentTimeMillis();
    //asyncTask.task1();
    //asyncTask.task2();
    //asyncTask.task3();
    Future<String> result1 = asyncTask.task4();
    Future<String> result2 = asyncTask.task5();
    Future<String> result3 = asyncTask.task6();
    System.out.println("返回結(jié)果:" + result1.get() + "," + result2.get() + "," + result3.get());
    for(;;) {
      if(result1.isDone() && result2.isDone() && result3.isDone()) {
        break;
      }
    }
    long end = System.currentTimeMillis();
    long total = end - begin;
    System.out.println("總耗時(shí):" + total);
    return "總耗時(shí):" + total;
  }
}

3.添加啟動(dòng)類

package top.ytheng.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication //等于下面3個(gè)
//@SpringBootConfiguration
//@EnableAutoConfiguration
//@ComponentScan
//攔截器用到
@ServletComponentScan
//MyBatis用到
@MapperScan("top.ytheng.demo.mapper")
//定時(shí)使用(開啟定時(shí)任務(wù))
@EnableScheduling
//開啟異步任務(wù)
@EnableAsync
public class DemoApplication {

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
}

4.右鍵項(xiàng)目Run As啟動(dòng),訪問url

http://localhost:8080/api/v1/async/test

結(jié)果:

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

相關(guān)文章

  • 一文帶你深入了解Java8 Stream流式編程

    一文帶你深入了解Java8 Stream流式編程

    在實(shí)際項(xiàng)目當(dāng)中,若能熟練使用Java8 的Stream流特性進(jìn)行開發(fā),就比較容易寫出簡(jiǎn)潔優(yōu)雅的代碼。本文主要就是基于實(shí)際項(xiàng)目常用的Stream Api流式處理總結(jié),希望對(duì)大家有所幫助
    2023-04-04
  • Spring使用ThreadPoolTaskExecutor自定義線程池及異步調(diào)用方式

    Spring使用ThreadPoolTaskExecutor自定義線程池及異步調(diào)用方式

    這篇文章主要介紹了Spring使用ThreadPoolTaskExecutor自定義線程池及異步調(diào)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 利用POI生成EXCEL文件的方法實(shí)例

    利用POI生成EXCEL文件的方法實(shí)例

    Apache POI 是用Java編寫的免費(fèi)開源的跨平臺(tái)的 Java API,Apache POI提供API給Java程式對(duì)Microsoft Office格式檔案讀和寫的功能,下面這篇文章主要給大家介紹了關(guān)于利用POI生成EXCEL文件的相關(guān)資料,需要的朋友可以參考下
    2018-07-07
  • SpringBoot讀取Resource目錄下文件的四種方式總結(jié)

    SpringBoot讀取Resource目錄下文件的四種方式總結(jié)

    在Spring?Boot項(xiàng)目中,經(jīng)常需要獲取resources目錄下的文件,這些文件可以包括配置文件、模板文件、靜態(tài)資源等,本文將介紹四種常用的方法來獲取resources目錄下的文件,需要的朋友可以參考下
    2023-08-08
  • 快速解決List集合add元素,添加多個(gè)對(duì)象出現(xiàn)重復(fù)的問題

    快速解決List集合add元素,添加多個(gè)對(duì)象出現(xiàn)重復(fù)的問題

    這篇文章主要介紹了快速解決List集合add元素,添加多個(gè)對(duì)象出現(xiàn)重復(fù)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • Java創(chuàng)建子線程的兩種方法

    Java創(chuàng)建子線程的兩種方法

    這篇文章主要介紹了Java創(chuàng)建子線程的兩種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • Java transient關(guān)鍵字與序列化操作實(shí)例詳解

    Java transient關(guān)鍵字與序列化操作實(shí)例詳解

    這篇文章主要介紹了Java transient關(guān)鍵字與序列化操作,結(jié)合實(shí)例形式詳細(xì)分析了java序列化操作相關(guān)實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下
    2019-09-09
  • Java Scala偏函數(shù)與偏應(yīng)用函數(shù)超詳細(xì)講解

    Java Scala偏函數(shù)與偏應(yīng)用函數(shù)超詳細(xì)講解

    Scala是一種多范式的編程語(yǔ)言,支持面向?qū)ο蠛秃瘮?shù)式編程。Scala也支持異常處理,即在程序運(yùn)行過程中發(fā)生意外或錯(cuò)誤時(shí),采取相應(yīng)的措施
    2023-04-04
  • springcloud + mybatis + seate集成示例

    springcloud + mybatis + seate集成示例

    本文主要介紹了springcloud + mybatis + seate集成示例,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-06-06
  • Spring的Bean生命周期之BeanDefinition詳解

    Spring的Bean生命周期之BeanDefinition詳解

    這篇文章主要介紹了Spring的Bean生命周期之BeanDefinition詳解,在spring bean創(chuàng)建過程 依賴 BeanDefinition 中的信息處理bean的生產(chǎn),BeanDefinition 是 Spring Framework 中定義 Bean 的配置元信息接口,需要的朋友可以參考下
    2023-12-12

最新評(píng)論