66套java從入門到精通實戰課程分享java
在後端開發中常常遇到一些耗時或者第三方系統調用的狀況,咱們知道Java程序通常的執行流程是順序執行(不考慮多線程併發的狀況),可是順序執行的效率確定是沒法達到咱們的預期的,這時就指望能夠並行執行,常規的作法是使用多線程或線程池,須要額外編寫代碼實現。在spring3.0後引入了@Async註解,使用該註解能夠達到線程池的執行效果,並且在開發上很是簡單。web
1、概述springboot是基於spring框架的,在springboot環境下演示@Async註解的使用方式。先看下該註解的定義,spring
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Async {
/**
* A qualifier value for the specified asynchronous operation(s).
* <p>May be used to determine the target executor to be used when executing this
* method, matching the qualifier value (or the bean name) of a specific
* {@link java.util.concurrent.Executor Executor} or
* {@link org.springframework.core.task.TaskExecutor TaskExecutor}
* bean definition.
* <p>When specified on a class level {@code @Async} annotation, indicates that the
* given executor should be used for all methods within the class. Method level use
* of {@code Async#value} always overrides any value set at the class level.
* @since 3.1.2
*/
String value() default "";
}
能夠看到該註解只有一個屬性,那就是value,從註釋上知道value指定的是執行該任務的線程池,也就是說咱們可使用子定義的線程池執行咱們的任務,而不是系統默認的。在看該註解上的註解,c#
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
也就是說該註解能夠用在方法和類上。標記在類上表示類中的全部方法都以異步方式執行,也就是提交到線程池執行。後端
2、詳述上面簡單對@Async註解進行了解釋,下面看用法。springboot
在springboot中要使用@Async註解必須在springboot啓動類上使用@EnableAsync註解,開啓@Async註解的自動配置,以下,多線程
package com.example.demo;
import com.example.demo.properties.ApplicationPro;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableConfigurationProperties({ApplicationPro.class})
//開啓@Async註解的自動配置
@EnableAsync
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
只有在啓動類上使用@EnableAsync註解,@Async註解纔會生效。併發
上面使用@EnableAsync註解已經開啓了對@Async註解的配置,下面看具體的異步調用類,app
package com.example.demo.service;
import com.example.demo.Student;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import java.util.concurrent.Future;
@Service
@Async
public class AsyncService {
public Future<Student> get(){
Student stu=new Student("1","3");
try {
Thread.sleep(10000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
return AsyncResult.forValue(stu);
}
public void executeRemote(){
try {
Thread.sleep(10000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
首先,要使該類讓spring管理必須使用@Service註解(或其餘註解也能夠),而後在類上標記@Async註解,前面說過@Async註解能夠在方法或類上使用,在類上使用則表示類中的全部方法均使用異步執行的方式。異步執行類中有兩個方法,每一個方法爲了演示執行的耗時操做均睡眠10s。這兩個方法一個是有返回值的,另外一個是無返回值的,重點看有返回值的,框架
public Future<Student> get(){
Student stu=new Student("1","3");
try {
Thread.sleep(10000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
return AsyncResult.forValue(stu);
}
爲何方法的返回值是Future,在@Async註釋上有下面這句話,
從上面的註解正好能夠說明返回Future是沒問題,可是咱們的方法就是一個普通的方法,要怎麼才能返回Future類那,不慌,spring針對@Async註解提供了AsyncResult類,從類名就知道該類就是爲了@Async註解準備的,
使用其中的forValue方法,即可以返回一個帶有泛型的Future類了。
看下測試類,
package com.example.demo.controller;
import com.example.demo.Student;
import com.example.demo.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
@Controller
@RequestMapping("async")
public class ControllerAsyncTest {
@Autowired
private AsyncService asyncService;
@RequestMapping("/test")
@ResponseBody
public Student get(){
try {
long start=System.currentTimeMillis(); //調用帶有返回值的get方法
Future<Student> result=asyncService.get(); //調用無返回值的executeRemote方法
asyncService.executeRemote();
Student student=result.get();
long end=System.currentTimeMillis();
System.out.println("執行時間:"+(end-start));
return student;
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return null;
}
}
測試類就是一個簡單的controller,調用了get和executeRemote方法,這兩個方法分別會睡眠10s,並且get會有返回值,下面看是否能夠拿到get的返回值,並看下調用這兩個方法的時間,
能夠成功拿到返回值,看執行時間,
2020-12-12 21:37:43.556 INFO 11780 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 5 ms
執行時間:10006
執行時間是10006ms,也就是10s多,按照上面的分析兩個方法分別睡眠了10s,若是同步執行那確定是20s,把@Async註解去掉看執行時間,
2020-12-12 21:41:07.840 INFO 11584 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 5 ms
執行時間:20001
執行時間是20001ms,算上兩個方法睡眠的時間,和測試類自己的時間,20001ms是沒錯的。從這裏能夠看出@Async註解的做用,把每一個方法看成任務提交給了線程池,提升了任務執行的時間。
另外,在獲取異步的執行結果使用了下面的方法,
Future<Student> result=asyncService.get();
asyncService.executeRemote();
//得到執行結果
Student student=result.get();
因爲在主線程要得到任務的執行結果,使用Future類的get方法得到結果,該結果須要等到任務執行完之後才能夠得到。
3、總結本文講解了異步調用@Async註解的使用,
一、使用@EnableAsync註解開啓對@Async註解的支持;
二、在類或方法上使用@Async註解;