引言: 在Java應用中,絕大多數狀況下都是經過同步的方式來實現交互處理的;可是在處理與第三方系統交互的時候,容易形成響應遲緩的狀況,以前大部分都是使用多線程來完成此類任務,其實,在spring 3.x以後,就已經內置了@Async來完美解決這個問題,本文將介紹在springboot中如何使用@Async。web
一、pom.xml中導入必要的依賴:spring
<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>
二、寫一個springboot的啓動類:tomcat
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.scheduling.annotation.EnableAsync; @ComponentScan(basePackages = { "com.xwj.controller", "com.xwj.service" }) @EnableAsync //開啓異步調用 @EnableAutoConfiguration public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
注意在這裏必定要加上@EnableAsync註解開啓異步調用springboot
三、建一個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.xwj.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"; } }
四、建一個service包,而後新建一個UserService類:app
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"); } }
先注掉@EnableAsync和@Async兩個註解,看下同步調用執行的效果。執行結果以下:異步
####IndexController#### 1 ####sendSms#### 2 ####sendSms#### 3 ####IndexController#### 4
對於sendSms方法,咱們並不關注它何時執行完,因此能夠採用異步的方式去執行。放開@EnableAsync和@Async兩個註解,執行結果以下:async
####IndexController#### 1 ####IndexController#### 4 ####sendSms#### 2 ####sendSms#### 3
bingo!達到了咱們預期的效果spring-boot
總結:spa
使用了@Async的方法,會被當成是一個子線程,全部整個sendSms方法,會在主線程執行完了以後執行
同一個類中,一個方法調用另一個有@Async的方法,註解是不會生效的