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

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

 更新時(shí)間:2020年03月13日 12:29:17   作者:玉天恒  
這篇文章主要介紹了SpringBoot異步任務(wù)使用方法詳解,文中通過(guò)示例代碼介紹的非常詳細(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í)使用(開(kāi)啟定時(shí)任務(wù))
@EnableScheduling
//開(kāi)啟異步任務(wù)
@EnableAsync
public class DemoApplication {

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

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

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

結(jié)果:

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

相關(guān)文章

最新評(píng)論

寿阳县| 和田县| 普陀区| 大渡口区| 海林市| 渭源县| 滦平县| 济阳县| 兴山县| 巴南区| 象州县| 文安县| 内丘县| 鄂伦春自治旗| 高安市| 原阳县| 开远市| 临高县| 弋阳县| 通榆县| 黔南| 时尚| 乐东| 塔河县| 宁晋县| 江津市| 凤城市| 麻阳| 南华县| 梧州市| 鄂托克前旗| 雷波县| 盖州市| 河池市| 万全县| 桂林市| 克什克腾旗| 弥勒县| 瓦房店市| 县级市| 来安县|