一. 什么是異步調(diào)用拐云?
異步調(diào)用是相對于同步調(diào)用而言的留潦,同步調(diào)用是指程序按預(yù)定順序一步步執(zhí)行,每一步必須等到上一步執(zhí)行完后才能執(zhí)行觉啊,異步調(diào)用則無需等待上一步程序執(zhí)行完即可執(zhí)行能犯。
二. 如何實(shí)現(xiàn)異步調(diào)用鲫骗?
多線程,這是很多人第一眼想到的關(guān)鍵詞踩晶,沒錯执泰,多線程就是一種實(shí)現(xiàn)異步調(diào)用的方式。在非spring目項(xiàng)目中我們要實(shí)現(xiàn)異步調(diào)用的就是使用多線程方式渡蜻,可以自己實(shí)現(xiàn)Runable接口或者集成Thread類坦胶,或者使用jdk1.5以上提供了的Executors線程池。在spring 3.x之后,就已經(jīng)內(nèi)置了@Async來完美解決這個問題顿苇,下面將介紹在springboot中如何使用@Async。
三. 舉例說明(無須知道執(zhí)行結(jié)果):
1税弃、pom.xml中導(dǎo)入必要的依賴:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<!-- SpringBoot 核心組件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
</dependencies>
2纪岁、寫一個springboot的啟動類:
啟動類里面使用@EnableAsync注解開啟功能,自動掃描
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@EnableAsync
public class SpringBootAsyncApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAsyncApplication.class, args);
}
}
3则果、建一個service包幔翰,然后新建一個UserService類:
- 要在異步任務(wù)的類上寫@Component
- 在定義異步任務(wù)類寫@Async(寫在類上代表整個類都是異步,在方法加上代表該類異步執(zhí)行)
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Async
public void sendSms(){
System.out.println("####sendSms#### 2");
IntStream.range(0, 5).forEach(d -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
System.out.println("####sendSms#### 3");
}
}
4西壮、建一個controller包遗增,然后新建一個IndexController類,用來獲取請求:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jeffrey.service.UserService;
@RestController
public class IndexController {
@Autowired
private UserService userService;
@RequestMapping("/async")
public String async(){
System.out.println("####IndexController#### 1");
userService.sendSms();
System.out.println("####IndexController#### 4");
return "success";
}
}
先注掉@EnableAsync和@Async兩個注解款青,看下同步調(diào)用執(zhí)行的效果做修。執(zhí)行結(jié)果如下:
####IndexController#### 1
####sendSms#### 2
####sendSms#### 3
####IndexController#### 4
對于sendSms方法,我們并不關(guān)注它什么時候執(zhí)行完抡草,所以可以采用異步的方式去執(zhí)行饰及。放開@EnableAsync和@Async兩個注解,執(zhí)行結(jié)果如下:
####IndexController#### 1
####IndexController#### 4
####sendSms#### 2
####sendSms#### 3
總結(jié):使用了@Async的方法康震,會被當(dāng)成是一個子線程燎含,所有整個sendSms方法,會在主線程執(zhí)行完了之后執(zhí)行
四. 舉例說明(須知道執(zhí)行結(jié)果):
基于上面例子腿短,這里只貼核心代碼
1屏箍、啟動類
@SpringBootApplication
@EnableAsync //開啟異步任務(wù)
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
2、異步類
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
import java.util.concurrent.Future;
/**
* @ClassName AsyncTestTask
* @Author jeffrey
* @Description 異步任務(wù)業(yè)務(wù)類
**/
@Component
@Async
public class AsyncTestTask {
//獲取異步結(jié)果
public Future<String> task4() throws InterruptedException{
long begin = System.currentTimeMillis();
Thread.sleep(2000L);
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(3000L);
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(1000L);
long end = System.currentTimeMillis();
System.out.println("任務(wù)6耗時="+(end-begin));
return new AsyncResult<String>("任務(wù)6");
}
}
3橘忱、controller類
import com.ceair.service.AsyncTestTask;
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 java.util.concurrent.Future;
/**
* @ClassName UserController
* @Author jeffrey
* @Description User
**/
@RestController
@RequestMapping("/api")
public class UserController {
@Autowired
private AsyncTestTask asyncTestTask;
@GetMapping("userAsyncTask")
public String exeTask() throws InterruptedException{
long begin = System.currentTimeMillis();
Future<String> task4 = asyncTestTask.task4();
Future<String> task5 = asyncTestTask.task5();
Future<String> task6 = asyncTestTask.task6();
//如果都執(zhí)行往就可以跳出循環(huán),isDone方法如果此任務(wù)完成赴魁,true
for(;;){
if (task4.isDone() && task5.isDone() && task6.isDone()) {
break;
}
}
long end = System.currentTimeMillis();
long total = end-begin;
System.out.println("執(zhí)行總耗時="+total);
return String.valueOf(total);
}
}
執(zhí)行結(jié)果如下:
任務(wù)6耗時=1000
任務(wù)4耗時=2000
任務(wù)5耗時=3000
執(zhí)行總耗時=3012
總結(jié):
從上面示例我們可以看出:如果同步方法,那我們需要6秒鹦付,而異步執(zhí)行尚粘,我們只需要3秒左右,這就是異步的作用敲长。
1)要把異步任務(wù)封裝到類里面郎嫁,不能直接寫到Controller
2)增加Future<String> 返回結(jié)果 AsyncResult<String>("task執(zhí)行完成");
3)如果需要拿到結(jié)果 需要判斷全部的 task.isDone()