在后端开发中常常遇到一些耗时或者第三方系统调用的状况,咱们知道Java程序通常的执行流程是顺序执行(不考虑多线程并发的状况),可是顺序执行的效率确定是没法达到咱们的预期的,这时就指望能够并行执行,常规的作法是使用多线程或线程池,须要额外编写代码实现。在spring3.0后引入了@Async注解,使用该注解能够达到线程池的执行效果,并且在开发上很是简单。java
springboot是基于spring框架的,在springboot环境下演示@Async注解的使用方式。先看下该注解的定义,web
@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指定的是执行该任务的线程池,也就是说咱们能够使用子定义的线程池执行咱们的任务,而不是系统默认的。在看该注解上的注解,spring
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
也就是说该注解能够用在方法和类上。标记在类上表示类中的全部方法都以异步方式执行,也就是提交到线程池执行。c#
上面简单对@Async注解进行了解释,下面看用法。后端
在springboot中要使用@Async注解必须在springboot启动类上使用@EnableAsync注解,开启@Async注解的自动配置,以下,springboot
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注解的配置,下面看具体的异步调用类,并发
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。这两个方法一个是有返回值的,另外一个是无返回值的,重点看有返回值的,app
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方法得到结果,该结果须要等到任务执行完之后才能够得到。
本文讲解了异步调用@Async注解的使用,
一、使用@EnableAsync注解开启对@Async注解的支持;
二、在类或方法上使用@Async注解;
有不当之处,欢迎指正,谢谢!