色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

SpringBoot異步任務使用方法詳解

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

步驟,如圖所示:

SpringBoot異步任務使用方法詳解

1.添加異步任務業(yè)務類

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;//異步任務業(yè)務類@Component//標記此類是異步類,也可在方法中標記//不加,則類里面的方法為同步執(zhí)行@Asyncpublic 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 { @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('返回結果:' + 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//開啟異步任務@EnableAsyncpublic 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異步任務使用方法詳解

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

標簽: Spring
相關文章:
主站蜘蛛池模板: 毛片免费永久不卡视频观看 | 久久久久久久久久久久久久久久久久 | 国产日韩精品一区在线不卡 | 国产精品久久自在自线观看 | 国产一级片免费观看 | 国产高清在线精品 | 免费a级毛片视频 | 国产成人最新毛片基地 | 无限资源中文免费 | 久久青青草视频 | 日韩理论视频 | 女人张开腿让男人操 | 亚洲国产精品一区二区三区 | 国产精品黄页网站在线播放免费 | 亚洲欧美视频一级 | 亚洲国产精品线在线观看 | 成人夜色香网站在线观看 | 精品一区二区三区免费爱 | 国产精品亚洲一区在线播放 | 成人黄页网站 | 亚洲加勒比在线 | 欧美成人全部免费观看1314色 | 国产女人伦码一区二区三区不卡 | 久久tv免费国产高清 | 欧美怡红院免费全视频 | 草草久久97超级碰碰碰免费 | 欧美视频一区二区 | 香港三级日本三级人妇三级四 | 成人午夜性a一级毛片美女 成人午夜亚洲影视在线观看 | 国产成人综合久久精品红 | 一级一级特黄女人精品毛片 | 午夜国产 | 在线观看精品国产 | 久久精品成人免费看 | 欧美日韩视频一区二区 | 国产免费视屏 | 国产成人高清精品免费观看 | 欧美日韩精品一区二区免费看 | 国产偷国产偷亚洲高清午夜 | 久久手机在线视频 | 精品久久久久久久久中文字幕 |