国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring Boot集成教程之異步調用Async

Spring Boot集成教程之異步調用Async

2021-04-14 11:24再見尼羅河 Java教程

在項目中,當訪問其他人的接口較慢或者做耗時任務時,不想程序一直卡在耗時任務上,想程序能夠并行執行,我們可以使用多線程來并行的處理任務,也可以使用spring提供的異步處理方式@Async。需要的朋友們下面來一起看看吧。

前言

本文主要給大家介紹了關于Spring Boot集成之異步調用Async的相關內容,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧。

什么是異步調用?

異步調用是相對于同步調用而言的,同步調用是指程序按預定順序一步步執行,每一步必須等到上一步執行完后才能執行,異步調用則無需等待上一步程序執行完即可執行。

異步處理方式

  • 調用之后,不返回任何數據。
  • 調用之后,返回數據,通過Future來獲取返回數據

如何實現異步調用?

多線程,這是很多人第一眼想到的關鍵詞,沒錯,多線程就是一種實現異步調用的方式。

在非spring目項目中我們要實現異步調用的就是使用多線程方式,可以自己實現Runable接口或者集成Thread類,或者使用jdk1.5以上提供了的Executors線程池。

StrngBoot中則提供了很方便的方式執行異步調用。

按照官方示例開擼

代碼入下

maven依賴:

?
1
2
3
4
5
6
7
8
9
10
11
<parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>1.5.3.RELEASE</version>
</parent>
<dependencies>
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
</dependencies>

啟動類:添加@EnableAsync注解

?
1
2
3
4
5
6
7
8
@SpringBootApplication
@EnableAsync
public class Application{
 
 public static void main(String[] args) {
  SpringApplication.run(Application.class, args);
 }
}

Controller

只需在需要異步執行方法上添加@Async注解

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
@RestController
@RequestMapping("")
public class AsyncTaskController {
 @RequestMapping("")
 public String doTask() throws InterruptedException{
  long currentTimeMillis = System.currentTimeMillis();
  this.task1();
  this.task2();
  this.task3();
  long currentTimeMillis1 = System.currentTimeMillis();
  return "task任務總耗時:"+(currentTimeMillis1-currentTimeMillis)+"ms";
 }
  
 @Async
 public void task1() throws InterruptedException{
  long currentTimeMillis = System.currentTimeMillis();
  Thread.sleep(1000);
  long currentTimeMillis1 = System.currentTimeMillis();
  System.out.println("task1任務耗時:"+(currentTimeMillis1-currentTimeMillis)+"ms");
 }
  
 @Async
 public void task2() throws InterruptedException{
  long currentTimeMillis = System.currentTimeMillis();
  Thread.sleep(2000);
  long currentTimeMillis1 = System.currentTimeMillis();
  System.out.println("task2任務耗時:"+(currentTimeMillis1-currentTimeMillis)+"ms");
 }
 @Async
 public void task3() throws InterruptedException{
  long currentTimeMillis = System.currentTimeMillis();
  Thread.sleep(3000);
  long currentTimeMillis1 = System.currentTimeMillis();
  System.out.println("task3任務耗時:"+(currentTimeMillis1-currentTimeMillis)+"ms");
 }
}

main函數運行spirngboot項目,啟動完成后瀏覽器訪問:http://localhost:8080/

控制臺:

?
1
2
3
task1任務耗時:1012ms
task2任務耗時:2009ms
task3任務耗時:3004ms

等了一段瀏覽器時候輸出入下:

?
1
task任務總耗時:6002ms

異步并沒有執行!

難道是代碼寫錯了?反復檢查了好幾遍,并沒有發現什么明顯錯誤,想起spring對@Transactional注解時也有類似問題,spring掃描時具有@Transactional注解方法的類時,是生成一個代理類,由代理類去開啟關閉事務,而在同一個類中,方法調用是在類體內執行的,spring無法截獲這個方法調用。

豁然開朗,將異步任務單獨放到一個類中,調整代碼入下:

Controller

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@RequestMapping("")
@RestController
public class AsyncTaskController {
 @Autowired
 private AsyncTask asyncTask;
 @RequestMapping("")
 public String doTask() throws InterruptedException{
  long currentTimeMillis = System.currentTimeMillis();
  asyncTask.task1();
  asyncTask.task2();
  asyncTask.task3();
  long currentTimeMillis1 = System.currentTimeMillis();
  return "task任務總耗時:"+(currentTimeMillis1-currentTimeMillis)+"ms";
   
 }
}

異步任務類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@Component
public class AsyncTask {
 @Async
 public void task1() throws InterruptedException{
  long currentTimeMillis = System.currentTimeMillis();
  Thread.sleep(1000);
  long currentTimeMillis1 = System.currentTimeMillis();
  System.out.println("task1任務耗時:"+(currentTimeMillis1-currentTimeMillis)+"ms");
 }
  
 @Async
 public void task2() throws InterruptedException{
  long currentTimeMillis = System.currentTimeMillis();
  Thread.sleep(2000);
  long currentTimeMillis1 = System.currentTimeMillis();
  System.out.println("task2任務耗時:"+(currentTimeMillis1-currentTimeMillis)+"ms");
 }
 @Async
 public void task3() throws InterruptedException{
  long currentTimeMillis = System.currentTimeMillis();
  Thread.sleep(3000);
  long currentTimeMillis1 = System.currentTimeMillis();
  System.out.println("task3任務耗時:"+(currentTimeMillis1-currentTimeMillis)+"ms");
 }
}

控制臺:

?
1
2
3
task1任務耗時:1012ms
task2任務耗時:2009ms
task3任務耗時:3004ms

訪問瀏覽器結果入下:

?
1
task任務總耗時:19ms

異步調用成功!

如何知道三個異步任務什么時候執行完,執行的結果怎樣呢?可以采用添加Fature回調方式判斷

代碼入下:

異步任務類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@Component
public class AsyncTask {
 @Async
 public Future<String> task1() throws InterruptedException{
  long currentTimeMillis = System.currentTimeMillis();
  Thread.sleep(1000);
  long currentTimeMillis1 = System.currentTimeMillis();
  System.out.println("task1任務耗時:"+(currentTimeMillis1-currentTimeMillis)+"ms");
  return new AsyncResult<String>("task1執行完畢");
 }
  
 @Async
 public Future<String> task2() throws InterruptedException{
  long currentTimeMillis = System.currentTimeMillis();
  Thread.sleep(2000);
  long currentTimeMillis1 = System.currentTimeMillis();
  System.out.println("task2任務耗時:"+(currentTimeMillis1-currentTimeMillis)+"ms");
  return new AsyncResult<String>("task2執行完畢");
 }
 @Async
 public Future<String> task3() throws InterruptedException{
  long currentTimeMillis = System.currentTimeMillis();
  Thread.sleep(3000);
  long currentTimeMillis1 = System.currentTimeMillis();
  System.out.println("task3任務耗時:"+(currentTimeMillis1-currentTimeMillis)+"ms");
  return new AsyncResult<String>("task3執行完畢");
 }
}

Controller

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@RequestMapping("")
@RestController
public class AsyncTaskController {
 @Autowired
 private AsyncTask asyncTask;
 @RequestMapping("")
 public String doTask() throws InterruptedException{
  long currentTimeMillis = System.currentTimeMillis();
  Future<String> task1 = asyncTask.task1();
  Future<String> task2 = asyncTask.task2();
  Future<String> task3 = asyncTask.task3();
  String result = null;
  for (;;) {
   if(task1.isDone() && task2.isDone() && task3.isDone()) {
    // 三個任務都調用完成,退出循環等待
    break;
   }
   Thread.sleep(1000);
  }
  long currentTimeMillis1 = System.currentTimeMillis();
  result = "task任務總耗時:"+(currentTimeMillis1-currentTimeMillis)+"ms";
  return result;
 }
}

控制臺輸出:

?
1
2
3
task1任務耗時:1000ms
task2任務耗時:2001ms
task3任務耗時:3001ms

瀏覽器輸出:

?
1
task任務總耗時:4015ms

異步調用成功,并且在所有任務都完成時程序才返回了結果!

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

原文鏈接:http://blog.csdn.net/v2sking/article/details/72795742

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 99久久久 | 99精品久久久久久久免费 | 黄久久久 | 欧美激情精品久久久久久变态 | 亚洲高清色综合 | 黄色一级毛片在线观看 | 日日操操 | 成人午夜精品久久久久久久网站 | 国产资源在线视频 | 精品女同一区二区三区在线绯色 | 亚洲国产精品激情在线观看 | 欧美成年网站 | 天堂资源 | 欧美另类视频在线 | 精品久久久久国产 | 精品国产欧美一区二区 | 日韩无在线| 国产大片在线观看 | 国产一区二区三区免费在线 | 午夜大片男女免费观看爽爽爽尤物 | 黄色影视| 日韩有码在线视频 | 97av在线| 久久久精品网站 | 成人免费乱码大片a毛片软件 | 亚洲久久久久久 | 91cn在线观看 | 国产乱码精品一区二区三区五月婷 | 午夜av影视| 成人性生交大片免费网站 | a黄视频| 日本一区二区在线观看视频 | 色接久久 | 成人黄色短视频在线观看 | 色视频www在线播放国产人成 | 国产三级网站 | 亚洲国产精品电影在线观看 | 九色视频网站 | 国产精国产精品 | 国产在线免费 | 高清一区在线 |