SpringBoot異步任務(wù)使用方法詳解
步驟,如圖所示:

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)文章
springboot集成nacos報(bào)錯(cuò):get data from Nacos
這篇文章給大家介紹了springboot集成nacos報(bào)錯(cuò):get data from Nacos error,dataId:null.yaml的原因及解決方法,如果又遇到相同問(wèn)題的朋友可以參考閱讀本文2023-10-10
SpringBoot使用RestTemplate實(shí)現(xiàn)HTTP請(qǐng)求詳解
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何使用RestTemplate實(shí)現(xiàn)進(jìn)行HTTP請(qǐng)求,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
Java 字符串截取及常見(jiàn)場(chǎng)景與方法詳解
在 Java 開(kāi)發(fā)中,截取字符串是一個(gè)非常常見(jiàn)的操作,無(wú)論是獲取文件名還是提取某些特定內(nèi)容,本文詳細(xì)介紹了截取字符串最后一位及其他常見(jiàn)截取操作的多種方法,幫助開(kāi)發(fā)者快速上手,感興趣的朋友跟隨小編一起看看吧2024-12-12
對(duì)SpringBoot項(xiàng)目Jar包進(jìn)行加密防止反編譯
最近項(xiàng)目要求部署到其他公司的服務(wù)器上,但是又不想將源碼泄露出去,要求對(duì)正式環(huán)境的啟動(dòng)包進(jìn)行安全性處理,防止客戶直接通過(guò)反編譯工具將代碼反編譯出來(lái),本文介紹了如何對(duì)SpringBoot項(xiàng)目Jar包進(jìn)行加密防止反編譯,需要的朋友可以參考下2023-10-10
這一次搞懂Spring代理創(chuàng)建及AOP鏈?zhǔn)秸{(diào)用過(guò)程操作
這篇文章主要介紹了這一次搞懂Spring代理創(chuàng)建及AOP鏈?zhǔn)秸{(diào)用過(guò)程操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
java高級(jí)用法之綁定CPU的線程Thread?Affinity簡(jiǎn)介
java線程thread affinity是用來(lái)將java代碼中的線程綁定到CPU特定的核上,用來(lái)提升程序運(yùn)行的性能,這篇文章主要介紹了java高級(jí)用法之綁定CPU的線程thread affinity的相關(guān)知識(shí),需要的朋友可以參考下2022-05-05
Spring Security其它權(quán)限校驗(yàn)方式&自定義權(quán)限校驗(yàn)方式
這篇文章主要介紹了Spring Security其它權(quán)限校驗(yàn)方式&自定義權(quán)限校驗(yàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
idea中Java實(shí)體類怎樣生成序列化的版本號(hào)的方法
這篇文章主要介紹了idea中Java實(shí)體類怎樣生成序列化的版本號(hào)的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
SpringBoot項(xiàng)目打包發(fā)布到外部tomcat(出現(xiàn)各種異常的解決)
這篇文章主要介紹了SpringBoot項(xiàng)目打包發(fā)布到外部tomcat(出現(xiàn)各種異常的解決),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09

