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耗時:" + (end - begin));
}
public void task2() throws InterruptedException {
long begin = System.currentTimeMillis();
Thread.sleep(2000);
long end = System.currentTimeMillis();
System.out.println("任務(wù)2耗時:" + (end - begin));
}
public void task3() throws InterruptedException {
long begin = System.currentTimeMillis();
Thread.sleep(3000);
long end = System.currentTimeMillis();
System.out.println("任務(wù)3耗時:" + (end - begin));
}
//測試拿到返回結(jié)果
public Future<String> task4() throws InterruptedException {
long begin = System.currentTimeMillis();
Thread.sleep(1000);
long end = System.currentTimeMillis();
System.out.println("任務(wù)4耗時:" + (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耗時:" + (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耗時:" + (end - begin));
return new AsyncResult<String>("任務(wù)6");
}
}
2.添加測試控制器
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("總耗時:" + total);
return "總耗時:" + total;
}
}
3.添加啟動類
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個
//@SpringBootConfiguration
//@EnableAutoConfiguration
//@ComponentScan
//攔截器用到
@ServletComponentScan
//MyBatis用到
@MapperScan("top.ytheng.demo.mapper")
//定時使用(開啟定時任務(wù))
@EnableScheduling
//開啟異步任務(wù)
@EnableAsync
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
4.右鍵項目Run As啟動,訪問url
http://localhost:8080/api/v1/async/test
結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot集成nacos報錯:get data from Nacos
這篇文章給大家介紹了springboot集成nacos報錯:get data from Nacos error,dataId:null.yaml的原因及解決方法,如果又遇到相同問題的朋友可以參考閱讀本文2023-10-10
SpringBoot使用RestTemplate實現(xiàn)HTTP請求詳解
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何使用RestTemplate實現(xiàn)進(jìn)行HTTP請求,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
對SpringBoot項目Jar包進(jìn)行加密防止反編譯
最近項目要求部署到其他公司的服務(wù)器上,但是又不想將源碼泄露出去,要求對正式環(huán)境的啟動包進(jìn)行安全性處理,防止客戶直接通過反編譯工具將代碼反編譯出來,本文介紹了如何對SpringBoot項目Jar包進(jìn)行加密防止反編譯,需要的朋友可以參考下2023-10-10
這一次搞懂Spring代理創(chuàng)建及AOP鏈?zhǔn)秸{(diào)用過程操作
這篇文章主要介紹了這一次搞懂Spring代理創(chuàng)建及AOP鏈?zhǔn)秸{(diào)用過程操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
java高級用法之綁定CPU的線程Thread?Affinity簡介
java線程thread affinity是用來將java代碼中的線程綁定到CPU特定的核上,用來提升程序運行的性能,這篇文章主要介紹了java高級用法之綁定CPU的線程thread affinity的相關(guān)知識,需要的朋友可以參考下2022-05-05
Spring Security其它權(quán)限校驗方式&自定義權(quán)限校驗方式
這篇文章主要介紹了Spring Security其它權(quán)限校驗方式&自定義權(quán)限校驗方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
SpringBoot項目打包發(fā)布到外部tomcat(出現(xiàn)各種異常的解決)
這篇文章主要介紹了SpringBoot項目打包發(fā)布到外部tomcat(出現(xiàn)各種異常的解決),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09

