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.右鍵項(xiàng)目Run As啟動,訪問url
http://localhost:8080/api/v1/async/test
結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring使用ThreadPoolTaskExecutor自定義線程池及異步調(diào)用方式
這篇文章主要介紹了Spring使用ThreadPoolTaskExecutor自定義線程池及異步調(diào)用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
SpringBoot讀取Resource目錄下文件的四種方式總結(jié)
在Spring?Boot項(xiàng)目中,經(jīng)常需要獲取resources目錄下的文件,這些文件可以包括配置文件、模板文件、靜態(tài)資源等,本文將介紹四種常用的方法來獲取resources目錄下的文件,需要的朋友可以參考下2023-08-08
快速解決List集合add元素,添加多個對象出現(xiàn)重復(fù)的問題
這篇文章主要介紹了快速解決List集合add元素,添加多個對象出現(xiàn)重復(fù)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
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ì)講解
Scala是一種多范式的編程語言,支持面向?qū)ο蠛秃瘮?shù)式編程。Scala也支持異常處理,即在程序運(yùn)行過程中發(fā)生意外或錯誤時,采取相應(yīng)的措施2023-04-04
springcloud + mybatis + seate集成示例
本文主要介紹了springcloud + mybatis + seate集成示例,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06
Spring的Bean生命周期之BeanDefinition詳解
這篇文章主要介紹了Spring的Bean生命周期之BeanDefinition詳解,在spring bean創(chuàng)建過程 依賴 BeanDefinition 中的信息處理bean的生產(chǎn),BeanDefinition 是 Spring Framework 中定義 Bean 的配置元信息接口,需要的朋友可以參考下2023-12-12

