日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot------异步任务的使用

發布時間:2025/6/15 javascript 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot------异步任务的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

步驟,如圖所示:

1.添加異步任務業務類

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;//異步任務業務類 @Component //標記此類是異步類,也可在方法中標記 //不加,則類里面的方法為同步執行 @Async public class AsyncTask {public void task1() throws InterruptedException {long begin = System.currentTimeMillis();Thread.sleep(1000);long end = System.currentTimeMillis();System.out.println("任務1耗時:" + (end - begin));}public void task2() throws InterruptedException {long begin = System.currentTimeMillis();Thread.sleep(2000);long end = System.currentTimeMillis();System.out.println("任務2耗時:" + (end - begin));}public void task3() throws InterruptedException {long begin = System.currentTimeMillis();Thread.sleep(3000);long end = System.currentTimeMillis();System.out.println("任務3耗時:" + (end - begin));}//測試拿到返回結果public Future<String> task4() throws InterruptedException {long begin = System.currentTimeMillis();Thread.sleep(1000);long end = System.currentTimeMillis();System.out.println("任務4耗時:" + (end - begin));return new AsyncResult<String>("任務4");}public Future<String> task5() throws InterruptedException {long begin = System.currentTimeMillis();Thread.sleep(2000);long end = System.currentTimeMillis();System.out.println("任務5耗時:" + (end - begin));return new AsyncResult<String>("任務5");}public Future<String> task6() throws InterruptedException {long begin = System.currentTimeMillis();Thread.sleep(3000);long end = System.currentTimeMillis();System.out.println("任務6耗時:" + (end - begin));return new AsyncResult<String>("任務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 {@Autowiredprivate 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("返回結果:" + 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") //定時使用(開啟定時任務) @EnableScheduling //開啟異步任務 @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

結果:

?

總結

以上是生活随笔為你收集整理的SpringBoot------异步任务的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。