午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術(shù)文章
文章詳情頁

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

瀏覽:157日期:2023-05-28 14:40:37

步驟,如圖所示:

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í)行@Asyncpublic 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ù)@EnableAsyncpublic 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é)果:

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 亚洲国产午夜 | 青青av在线 | 精品麻豆视频 | 一区二区三区蜜桃 | 亚洲国产91 | 国产又大又黄又粗 | 一级黄色片网址 | 日韩网站视频 | 久草视频手机在线 | 午夜三级视频 | 欧美乱子伦 | 2019av| 日本天天色 | 久久久久久久久免费视频 | 超碰免费公开在线 | 欧美日韩视频在线 | 99精品视频免费 | 欧美在线播放一区 | 91爱爱| 影音先锋中文在线 | 亚洲第一视频网 | 黄色网址你懂的 | 嫩草影院污| 在线视频观看你懂的 | 毛片网站免费 | 成年人不懂如何谈恋爱免费观看 | 黄色一级视频免费 | 91丨porny丨露出| 亚洲精品欧美精品 | 日本www免费 | 在线观看亚洲天堂 | 91水蜜桃 | 一级片在线观看免费 | 激情视频一区 | 国产一区在线观看免费 | 亚洲天堂一区在线观看 | 超碰在线人人草 | 操操操操操| h片免费在线观看 | 深爱五月网 | 国产综合视频 |