Springboot--Async
package com.example.springbootasync.Controller; /* 任务一开始执行 任务二开始执行 任务三开始执行 耗时135 耗时573 耗时7282 */ import com.example.springbootasync.service.AsyncService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.nio.charset.Charset; import java.util.concurrent.Future; @RestController public class FirstController { @Autowired private AsyncService asyncService; @RequestMapping("/async") @ResponseBody public String asyncTest(Model model)throws Exception { long start=System.currentTimeMillis(); Future<String> tast1= asyncService.doTask1(); Future<String> tast2= asyncService.doTask2(); Future<String> tast3= asyncService.doTask3(); while (true){ if (tast1.isDone() && tast2.isDone() && tast3.isDone() ){ break; } Thread.sleep(1000); } long end=System.currentTimeMillis(); return "耗时"+(end-start); } }
package com.example.springbootasync.service; import java.util.concurrent.Future; public interface AsyncService { Future<String> doTask1()throws Exception; Future<String> doTask2()throws Exception; Future<String> doTask3()throws Exception; }
package com.example.springbootasync.service.impl; import com.example.springbootasync.service.AsyncService; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.AsyncResult; import org.springframework.stereotype.Service; import java.util.Random; import java.util.concurrent.Future; @Service public class AsyncServiceImpl implements AsyncService { private static Random random=new Random(); @Override @Async public Future<String> doTask1() throws Exception { System.out.println("任务一开始执行"); long start=System.currentTimeMillis(); Thread.sleep(random.nextInt(10000)); long end=System.currentTimeMillis(); System.out.println("耗时"+(end-start)); return new AsyncResult<>("任务一结束"); } @Async @Override public Future<String> doTask2() throws Exception { System.out.println("任务二开始执行"); long start=System.currentTimeMillis(); Thread.sleep(random.nextInt(10000)); long end=System.currentTimeMillis(); System.out.println("耗时"+(end-start)); return new AsyncResult<>("任务二结束"); } @Async @Override public Future<String> doTask3() throws Exception { System.out.println("任务三开始执行"); long start=System.currentTimeMillis(); Thread.sleep(random.nextInt(10000)); long end=System.currentTimeMillis(); System.out.println("耗时"+(end-start)); return new AsyncResult<>("任务三结束"); } }
package com.example.springbootasync; 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); } }
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>springboot-async</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot-async</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>