@@ -1,25 +0,0 @@ | |||
/target/ | |||
!.mvn/wrapper/maven-wrapper.jar | |||
### STS ### | |||
.apt_generated | |||
.classpath | |||
.factorypath | |||
.project | |||
.settings | |||
.springBeans | |||
.sts4-cache | |||
### IntelliJ IDEA ### | |||
.idea | |||
*.iws | |||
*.iml | |||
*.ipr | |||
### NetBeans ### | |||
/nbproject/private/ | |||
/build/ | |||
/nbbuild/ | |||
/dist/ | |||
/nbdist/ | |||
/.nb-gradle/ |
@@ -1,257 +0,0 @@ | |||
# spring-boot-demo-async | |||
> 此 demo 主要演示了 Spring Boot 如何使用原生提供的异步任务支持,实现异步执行任务。 | |||
## pom.xml | |||
```xml | |||
<?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> | |||
<artifactId>spring-boot-demo-async</artifactId> | |||
<version>1.0.0-SNAPSHOT</version> | |||
<packaging>jar</packaging> | |||
<name>spring-boot-demo-async</name> | |||
<description>Demo project for Spring Boot</description> | |||
<parent> | |||
<groupId>com.xkcoding</groupId> | |||
<artifactId>spring-boot-demo</artifactId> | |||
<version>1.0.0-SNAPSHOT</version> | |||
</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</artifactId> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.springframework.boot</groupId> | |||
<artifactId>spring-boot-starter-test</artifactId> | |||
<scope>test</scope> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.projectlombok</groupId> | |||
<artifactId>lombok</artifactId> | |||
<optional>true</optional> | |||
</dependency> | |||
</dependencies> | |||
<build> | |||
<finalName>spring-boot-demo-async</finalName> | |||
<plugins> | |||
<plugin> | |||
<groupId>org.springframework.boot</groupId> | |||
<artifactId>spring-boot-maven-plugin</artifactId> | |||
</plugin> | |||
</plugins> | |||
</build> | |||
</project> | |||
``` | |||
## application.yml | |||
```yaml | |||
spring: | |||
task: | |||
execution: | |||
pool: | |||
# 最大线程数 | |||
max-size: 16 | |||
# 核心线程数 | |||
core-size: 16 | |||
# 存活时间 | |||
keep-alive: 10s | |||
# 队列大小 | |||
queue-capacity: 100 | |||
# 是否允许核心线程超时 | |||
allow-core-thread-timeout: true | |||
# 线程名称前缀 | |||
thread-name-prefix: async-task- | |||
``` | |||
## SpringBootDemoAsyncApplication.java | |||
```java | |||
/** | |||
* <p> | |||
* 启动器 | |||
* </p> | |||
* | |||
* @author yangkai.shen | |||
* @date Created in 2018-12-29 10:28 | |||
*/ | |||
@EnableAsync | |||
@SpringBootApplication | |||
public class SpringBootDemoAsyncApplication { | |||
public static void main(String[] args) { | |||
SpringApplication.run(SpringBootDemoAsyncApplication.class, args); | |||
} | |||
} | |||
``` | |||
## TaskFactory.java | |||
```java | |||
/** | |||
* <p> | |||
* 任务工厂 | |||
* </p> | |||
* | |||
* @author yangkai.shen | |||
* @date Created in 2018-12-29 10:37 | |||
*/ | |||
@Component | |||
@Slf4j | |||
public class TaskFactory { | |||
/** | |||
* 模拟5秒的异步任务 | |||
*/ | |||
@Async | |||
public Future<Boolean> asyncTask1() throws InterruptedException { | |||
doTask("asyncTask1", 5); | |||
return new AsyncResult<>(Boolean.TRUE); | |||
} | |||
/** | |||
* 模拟2秒的异步任务 | |||
*/ | |||
@Async | |||
public Future<Boolean> asyncTask2() throws InterruptedException { | |||
doTask("asyncTask2", 2); | |||
return new AsyncResult<>(Boolean.TRUE); | |||
} | |||
/** | |||
* 模拟3秒的异步任务 | |||
*/ | |||
@Async | |||
public Future<Boolean> asyncTask3() throws InterruptedException { | |||
doTask("asyncTask3", 3); | |||
return new AsyncResult<>(Boolean.TRUE); | |||
} | |||
/** | |||
* 模拟5秒的同步任务 | |||
*/ | |||
public void task1() throws InterruptedException { | |||
doTask("task1", 5); | |||
} | |||
/** | |||
* 模拟2秒的同步任务 | |||
*/ | |||
public void task2() throws InterruptedException { | |||
doTask("task2", 2); | |||
} | |||
/** | |||
* 模拟3秒的同步任务 | |||
*/ | |||
public void task3() throws InterruptedException { | |||
doTask("task3", 3); | |||
} | |||
private void doTask(String taskName, Integer time) throws InterruptedException { | |||
log.info("{}开始执行,当前线程名称【{}】", taskName, Thread.currentThread().getName()); | |||
TimeUnit.SECONDS.sleep(time); | |||
log.info("{}执行成功,当前线程名称【{}】", taskName, Thread.currentThread().getName()); | |||
} | |||
} | |||
``` | |||
## TaskFactoryTest.java | |||
```java | |||
/** | |||
* <p> | |||
* 测试任务 | |||
* </p> | |||
* | |||
* @author yangkai.shen | |||
* @date Created in 2018-12-29 10:49 | |||
*/ | |||
@Slf4j | |||
public class TaskFactoryTest extends SpringBootDemoAsyncApplicationTests { | |||
@Autowired | |||
private TaskFactory task; | |||
/** | |||
* 测试异步任务 | |||
*/ | |||
@Test | |||
public void asyncTaskTest() throws InterruptedException, ExecutionException { | |||
long start = System.currentTimeMillis(); | |||
Future<Boolean> asyncTask1 = task.asyncTask1(); | |||
Future<Boolean> asyncTask2 = task.asyncTask2(); | |||
Future<Boolean> asyncTask3 = task.asyncTask3(); | |||
// 调用 get() 阻塞主线程 | |||
asyncTask1.get(); | |||
asyncTask2.get(); | |||
asyncTask3.get(); | |||
long end = System.currentTimeMillis(); | |||
log.info("异步任务全部执行结束,总耗时:{} 毫秒", (end - start)); | |||
} | |||
/** | |||
* 测试同步任务 | |||
*/ | |||
@Test | |||
public void taskTest() throws InterruptedException { | |||
long start = System.currentTimeMillis(); | |||
task.task1(); | |||
task.task2(); | |||
task.task3(); | |||
long end = System.currentTimeMillis(); | |||
log.info("同步任务全部执行结束,总耗时:{} 毫秒", (end - start)); | |||
} | |||
} | |||
``` | |||
## 运行结果 | |||
### 异步任务 | |||
```bash | |||
2018-12-29 10:57:28.511 INFO 3134 --- [ async-task-3] com.xkcoding.async.task.TaskFactory : asyncTask3开始执行,当前线程名称【async-task-3】 | |||
2018-12-29 10:57:28.511 INFO 3134 --- [ async-task-1] com.xkcoding.async.task.TaskFactory : asyncTask1开始执行,当前线程名称【async-task-1】 | |||
2018-12-29 10:57:28.511 INFO 3134 --- [ async-task-2] com.xkcoding.async.task.TaskFactory : asyncTask2开始执行,当前线程名称【async-task-2】 | |||
2018-12-29 10:57:30.514 INFO 3134 --- [ async-task-2] com.xkcoding.async.task.TaskFactory : asyncTask2执行成功,当前线程名称【async-task-2】 | |||
2018-12-29 10:57:31.516 INFO 3134 --- [ async-task-3] com.xkcoding.async.task.TaskFactory : asyncTask3执行成功,当前线程名称【async-task-3】 | |||
2018-12-29 10:57:33.517 INFO 3134 --- [ async-task-1] com.xkcoding.async.task.TaskFactory : asyncTask1执行成功,当前线程名称【async-task-1】 | |||
2018-12-29 10:57:33.517 INFO 3134 --- [ main] com.xkcoding.async.task.TaskFactoryTest : 异步任务全部执行结束,总耗时:5015 毫秒 | |||
``` | |||
### 同步任务 | |||
```bash | |||
2018-12-29 10:55:49.830 INFO 3079 --- [ main] com.xkcoding.async.task.TaskFactory : task1开始执行,当前线程名称【main】 | |||
2018-12-29 10:55:54.834 INFO 3079 --- [ main] com.xkcoding.async.task.TaskFactory : task1执行成功,当前线程名称【main】 | |||
2018-12-29 10:55:54.835 INFO 3079 --- [ main] com.xkcoding.async.task.TaskFactory : task2开始执行,当前线程名称【main】 | |||
2018-12-29 10:55:56.839 INFO 3079 --- [ main] com.xkcoding.async.task.TaskFactory : task2执行成功,当前线程名称【main】 | |||
2018-12-29 10:55:56.839 INFO 3079 --- [ main] com.xkcoding.async.task.TaskFactory : task3开始执行,当前线程名称【main】 | |||
2018-12-29 10:55:59.843 INFO 3079 --- [ main] com.xkcoding.async.task.TaskFactory : task3执行成功,当前线程名称【main】 | |||
2018-12-29 10:55:59.843 INFO 3079 --- [ main] com.xkcoding.async.task.TaskFactoryTest : 同步任务全部执行结束,总耗时:10023 毫秒 | |||
``` | |||
## 参考 | |||
- Spring Boot 异步任务线程池的配置 参考官方文档:https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#boot-features-task-execution-scheduling |
@@ -1,54 +0,0 @@ | |||
<?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> | |||
<artifactId>demo-async</artifactId> | |||
<version>1.0.0-SNAPSHOT</version> | |||
<packaging>jar</packaging> | |||
<name>demo-async</name> | |||
<description>Demo project for Spring Boot</description> | |||
<parent> | |||
<groupId>com.xkcoding</groupId> | |||
<artifactId>spring-boot-demo</artifactId> | |||
<version>1.0.0-SNAPSHOT</version> | |||
</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</artifactId> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.springframework.boot</groupId> | |||
<artifactId>spring-boot-starter-test</artifactId> | |||
<scope>test</scope> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.projectlombok</groupId> | |||
<artifactId>lombok</artifactId> | |||
<optional>true</optional> | |||
</dependency> | |||
</dependencies> | |||
<build> | |||
<finalName>demo-async</finalName> | |||
<plugins> | |||
<plugin> | |||
<groupId>org.springframework.boot</groupId> | |||
<artifactId>spring-boot-maven-plugin</artifactId> | |||
</plugin> | |||
</plugins> | |||
</build> | |||
</project> |
@@ -1,17 +0,0 @@ | |||
package com.xkcoding.async; | |||
import org.junit.Test; | |||
import org.junit.runner.RunWith; | |||
import org.springframework.boot.test.context.SpringBootTest; | |||
import org.springframework.test.context.junit4.SpringRunner; | |||
@RunWith(SpringRunner.class) | |||
@SpringBootTest | |||
public class SpringBootDemoAsyncApplicationTests { | |||
@Test | |||
public void contextLoads() { | |||
} | |||
} | |||
@@ -0,0 +1,209 @@ | |||
## spring-boot-demo-async | |||
> 此 demo 主要演示了 Spring Boot 如何使用原生提供的异步任务支持,实现异步执行任务。 | |||
### 1.开发步骤 | |||
#### 1.1.添加依赖 | |||
```xml | |||
<dependencies> | |||
<dependency> | |||
<groupId>org.springframework.boot</groupId> | |||
<artifactId>spring-boot-starter</artifactId> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.springframework.boot</groupId> | |||
<artifactId>spring-boot-starter-test</artifactId> | |||
<scope>test</scope> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.projectlombok</groupId> | |||
<artifactId>lombok</artifactId> | |||
<optional>true</optional> | |||
</dependency> | |||
</dependencies> | |||
``` | |||
#### 1.2.增加异步线程池的配置 | |||
> 注意:如果不配置的话,会使用默认的线程池配置 | |||
```yaml | |||
spring: | |||
task: | |||
execution: | |||
pool: | |||
# 最大线程数 | |||
max-size: 16 | |||
# 核心线程数 | |||
core-size: 16 | |||
# 存活时间 | |||
keep-alive: 10s | |||
# 队列大小 | |||
queue-capacity: 100 | |||
# 是否允许核心线程超时 | |||
allow-core-thread-timeout: true | |||
# 线程名称前缀 | |||
thread-name-prefix: async-task- | |||
``` | |||
#### 1.3.同步、异步任务模拟 | |||
> 1. 异步方法的返回值,需要指定为:`java.util.concurrent.Future`,`org.springframework.util.concurrent.ListenableFuture`,`java.util.concurrent.CompletableFuture` | |||
> 2. 方法上标记 `@Async` | |||
```java | |||
@Component | |||
@Slf4j | |||
public class MockTaskFactory { | |||
/** | |||
* 模拟5秒的异步任务 | |||
*/ | |||
@Async | |||
public CompletableFuture<Boolean> asyncTask1() throws InterruptedException { | |||
doTask("asyncTask1", 5); | |||
return CompletableFuture.completedFuture(Boolean.TRUE); | |||
} | |||
/** | |||
* 模拟2秒的异步任务 | |||
*/ | |||
@Async | |||
public CompletableFuture<Boolean> asyncTask2() throws InterruptedException { | |||
doTask("asyncTask2", 2); | |||
return CompletableFuture.completedFuture(Boolean.TRUE); | |||
} | |||
/** | |||
* 模拟3秒的异步任务 | |||
*/ | |||
@Async | |||
public CompletableFuture<Boolean> asyncTask3() throws InterruptedException { | |||
doTask("asyncTask3", 3); | |||
return CompletableFuture.completedFuture(Boolean.TRUE); | |||
} | |||
/** | |||
* 模拟5秒的同步任务 | |||
*/ | |||
public void task1() throws InterruptedException { | |||
doTask("task1", 5); | |||
} | |||
/** | |||
* 模拟2秒的同步任务 | |||
*/ | |||
public void task2() throws InterruptedException { | |||
doTask("task2", 2); | |||
} | |||
/** | |||
* 模拟3秒的同步任务 | |||
*/ | |||
public void task3() throws InterruptedException { | |||
doTask("task3", 3); | |||
} | |||
private void doTask(String taskName, Integer time) throws InterruptedException { | |||
log.info("{}开始执行,当前线程名称【{}】", taskName, Thread.currentThread().getName()); | |||
TimeUnit.SECONDS.sleep(time); | |||
log.info("{}执行成功,当前线程名称【{}】", taskName, Thread.currentThread().getName()); | |||
} | |||
} | |||
``` | |||
#### 1.4.在启动类上增加注解 `@EnableAsync` | |||
```java | |||
@EnableAsync | |||
@SpringBootApplication | |||
public class AsyncApplication { | |||
public static void main(String[] args) { | |||
SpringApplication.run(AsyncApplication.class, args); | |||
} | |||
} | |||
``` | |||
### 2.测试 | |||
#### 2.1.编写测试代码 | |||
```java | |||
@Slf4j | |||
@SpringBootTest | |||
@DisplayName("异步化测试") | |||
public class MockTaskFactoryTest { | |||
@Autowired | |||
private MockTaskFactory task; | |||
/** | |||
* 测试异步任务 | |||
*/ | |||
@Test | |||
@DisplayName("异步任务") | |||
public void asyncTaskTest() throws InterruptedException, ExecutionException { | |||
long start = System.currentTimeMillis(); | |||
CompletableFuture<Boolean> asyncResult1 = task.asyncTask1(); | |||
CompletableFuture<Boolean> asyncResult2 = task.asyncTask2(); | |||
CompletableFuture<Boolean> asyncResult3 = task.asyncTask3(); | |||
CompletableFuture<Void> allResult = CompletableFuture.allOf(asyncResult1, asyncResult2, asyncResult3); | |||
// 调用 get() 阻塞主线程 | |||
allResult.get(); | |||
long end = System.currentTimeMillis(); | |||
log.info("异步任务全部执行结束,总耗时:{} 毫秒", (end - start)); | |||
} | |||
/** | |||
* 测试同步任务 | |||
*/ | |||
@Test | |||
@DisplayName("同步任务") | |||
public void taskTest() throws InterruptedException { | |||
long start = System.currentTimeMillis(); | |||
task.task1(); | |||
task.task2(); | |||
task.task3(); | |||
long end = System.currentTimeMillis(); | |||
log.info("同步任务全部执行结束,总耗时:{} 毫秒", (end - start)); | |||
} | |||
} | |||
``` | |||
#### 2.2运行结果 | |||
- 异步任务 | |||
```bash | |||
INFO 11574 --- [ async-task-1] com.xkcoding.async.task.MockTaskFactory : asyncTask1开始执行,当前线程名称【async-task-1】 | |||
INFO 11574 --- [ async-task-2] com.xkcoding.async.task.MockTaskFactory : asyncTask2开始执行,当前线程名称【async-task-2】 | |||
INFO 11574 --- [ async-task-3] com.xkcoding.async.task.MockTaskFactory : asyncTask3开始执行,当前线程名称【async-task-3】 | |||
INFO 11574 --- [ async-task-2] com.xkcoding.async.task.MockTaskFactory : asyncTask2执行成功,当前线程名称【async-task-2】 | |||
INFO 11574 --- [ async-task-3] com.xkcoding.async.task.MockTaskFactory : asyncTask3执行成功,当前线程名称【async-task-3】 | |||
INFO 11574 --- [ async-task-1] com.xkcoding.async.task.MockTaskFactory : asyncTask1执行成功,当前线程名称【async-task-1】 | |||
INFO 11574 --- [ main] c.x.async.task.MockTaskFactoryTest : 异步任务全部执行结束,总耗时:5017 毫秒 | |||
``` | |||
- 同步任务 | |||
```bash | |||
INFO 11574 --- [ main] com.xkcoding.async.task.MockTaskFactory : task1开始执行,当前线程名称【main】 | |||
INFO 11574 --- [ main] com.xkcoding.async.task.MockTaskFactory : task1执行成功,当前线程名称【main】 | |||
INFO 11574 --- [ main] com.xkcoding.async.task.MockTaskFactory : task2开始执行,当前线程名称【main】 | |||
INFO 11574 --- [ main] com.xkcoding.async.task.MockTaskFactory : task2执行成功,当前线程名称【main】 | |||
INFO 11574 --- [ main] com.xkcoding.async.task.MockTaskFactory : task3开始执行,当前线程名称【main】 | |||
INFO 11574 --- [ main] com.xkcoding.async.task.MockTaskFactory : task3执行成功,当前线程名称【main】 | |||
INFO 11574 --- [ main] c.x.async.task.MockTaskFactoryTest : 同步任务全部执行结束,总耗时:10032 毫秒 | |||
``` | |||
### 3.参考 | |||
- Spring Boot 异步任务线程池的配置 参考官方文档:https://docs.spring.io/spring-boot/docs/3.0.0-M4/reference/htmlsingle/#features.task-execution-and-scheduling |
@@ -0,0 +1,52 @@ | |||
<?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"> | |||
<parent> | |||
<groupId>com.xkcoding</groupId> | |||
<artifactId>demo-base</artifactId> | |||
<version>1.0.0-SNAPSHOT</version> | |||
</parent> | |||
<modelVersion>4.0.0</modelVersion> | |||
<artifactId>demo-base-async</artifactId> | |||
<version>1.0.0-SNAPSHOT</version> | |||
<packaging>jar</packaging> | |||
<name>demo-base-async</name> | |||
<description>Demo project for Spring Boot</description> | |||
<properties> | |||
<java.version>17</java.version> | |||
</properties> | |||
<dependencies> | |||
<dependency> | |||
<groupId>org.springframework.boot</groupId> | |||
<artifactId>spring-boot-starter</artifactId> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.springframework.boot</groupId> | |||
<artifactId>spring-boot-starter-test</artifactId> | |||
<scope>test</scope> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.projectlombok</groupId> | |||
<artifactId>lombok</artifactId> | |||
<optional>true</optional> | |||
</dependency> | |||
</dependencies> | |||
<build> | |||
<finalName>demo-base-async</finalName> | |||
<plugins> | |||
<plugin> | |||
<groupId>org.springframework.boot</groupId> | |||
<artifactId>spring-boot-maven-plugin</artifactId> | |||
</plugin> | |||
</plugins> | |||
</build> | |||
</project> |
@@ -10,14 +10,14 @@ import org.springframework.scheduling.annotation.EnableAsync; | |||
* </p> | |||
* | |||
* @author yangkai.shen | |||
* @date Created in 2018-12-29 10:28 | |||
* @date Created in 2022-08-19 21:19 | |||
*/ | |||
@EnableAsync | |||
@SpringBootApplication | |||
public class SpringBootDemoAsyncApplication { | |||
public class AsyncApplication { | |||
public static void main(String[] args) { | |||
SpringApplication.run(SpringBootDemoAsyncApplication.class, args); | |||
SpringApplication.run(AsyncApplication.class, args); | |||
} | |||
} |
@@ -2,49 +2,48 @@ package com.xkcoding.async.task; | |||
import lombok.extern.slf4j.Slf4j; | |||
import org.springframework.scheduling.annotation.Async; | |||
import org.springframework.scheduling.annotation.AsyncResult; | |||
import org.springframework.stereotype.Component; | |||
import java.util.concurrent.Future; | |||
import java.util.concurrent.CompletableFuture; | |||
import java.util.concurrent.TimeUnit; | |||
/** | |||
* <p> | |||
* 任务工厂 | |||
* 模拟任务工厂 | |||
* </p> | |||
* | |||
* @author yangkai.shen | |||
* @date Created in 2018-12-29 10:37 | |||
* @date Created in 2022-08-19 21:19 | |||
*/ | |||
@Component | |||
@Slf4j | |||
public class TaskFactory { | |||
public class MockTaskFactory { | |||
/** | |||
* 模拟5秒的异步任务 | |||
*/ | |||
@Async | |||
public Future<Boolean> asyncTask1() throws InterruptedException { | |||
public CompletableFuture<Boolean> asyncTask1() throws InterruptedException { | |||
doTask("asyncTask1", 5); | |||
return new AsyncResult<>(Boolean.TRUE); | |||
return CompletableFuture.completedFuture(Boolean.TRUE); | |||
} | |||
/** | |||
* 模拟2秒的异步任务 | |||
*/ | |||
@Async | |||
public Future<Boolean> asyncTask2() throws InterruptedException { | |||
public CompletableFuture<Boolean> asyncTask2() throws InterruptedException { | |||
doTask("asyncTask2", 2); | |||
return new AsyncResult<>(Boolean.TRUE); | |||
return CompletableFuture.completedFuture(Boolean.TRUE); | |||
} | |||
/** | |||
* 模拟3秒的异步任务 | |||
*/ | |||
@Async | |||
public Future<Boolean> asyncTask3() throws InterruptedException { | |||
public CompletableFuture<Boolean> asyncTask3() throws InterruptedException { | |||
doTask("asyncTask3", 3); | |||
return new AsyncResult<>(Boolean.TRUE); | |||
return CompletableFuture.completedFuture(Boolean.TRUE); | |||
} | |||
/** |
@@ -0,0 +1,14 @@ | |||
package com.xkcoding.async; | |||
import org.junit.jupiter.api.Test; | |||
import org.springframework.boot.test.context.SpringBootTest; | |||
@SpringBootTest | |||
class AsyncApplicationTests { | |||
@Test | |||
void contextLoads() { | |||
} | |||
} | |||
@@ -1,40 +1,43 @@ | |||
package com.xkcoding.async.task; | |||
import com.xkcoding.async.SpringBootDemoAsyncApplicationTests; | |||
import lombok.extern.slf4j.Slf4j; | |||
import org.junit.Test; | |||
import org.junit.jupiter.api.DisplayName; | |||
import org.junit.jupiter.api.Test; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
import org.springframework.boot.test.context.SpringBootTest; | |||
import java.util.concurrent.CompletableFuture; | |||
import java.util.concurrent.ExecutionException; | |||
import java.util.concurrent.Future; | |||
/** | |||
* <p> | |||
* 测试任务 | |||
* 异步任务单元测试 | |||
* </p> | |||
* | |||
* @author yangkai.shen | |||
* @date Created in 2018-12-29 10:49 | |||
* @date Created in 2022-08-19 21:21 | |||
*/ | |||
@Slf4j | |||
public class TaskFactoryTest extends SpringBootDemoAsyncApplicationTests { | |||
@SpringBootTest | |||
@DisplayName("异步化测试") | |||
public class MockTaskFactoryTest { | |||
@Autowired | |||
private TaskFactory task; | |||
private MockTaskFactory task; | |||
/** | |||
* 测试异步任务 | |||
*/ | |||
@Test | |||
@DisplayName("异步任务") | |||
public void asyncTaskTest() throws InterruptedException, ExecutionException { | |||
long start = System.currentTimeMillis(); | |||
Future<Boolean> asyncTask1 = task.asyncTask1(); | |||
Future<Boolean> asyncTask2 = task.asyncTask2(); | |||
Future<Boolean> asyncTask3 = task.asyncTask3(); | |||
CompletableFuture<Boolean> asyncResult1 = task.asyncTask1(); | |||
CompletableFuture<Boolean> asyncResult2 = task.asyncTask2(); | |||
CompletableFuture<Boolean> asyncResult3 = task.asyncTask3(); | |||
CompletableFuture<Void> allResult = CompletableFuture.allOf(asyncResult1, asyncResult2, asyncResult3); | |||
// 调用 get() 阻塞主线程 | |||
asyncTask1.get(); | |||
asyncTask2.get(); | |||
asyncTask3.get(); | |||
allResult.get(); | |||
long end = System.currentTimeMillis(); | |||
log.info("异步任务全部执行结束,总耗时:{} 毫秒", (end - start)); | |||
@@ -44,6 +47,7 @@ public class TaskFactoryTest extends SpringBootDemoAsyncApplicationTests { | |||
* 测试同步任务 | |||
*/ | |||
@Test | |||
@DisplayName("同步任务") | |||
public void taskTest() throws InterruptedException { | |||
long start = System.currentTimeMillis(); | |||
task.task1(); |
@@ -21,6 +21,7 @@ | |||
<modules> | |||
<module>demo-base-helloworld</module> | |||
<module>demo-base-properties</module> | |||
<module>demo-base-async</module> | |||
</modules> | |||
</project> |
@@ -65,7 +65,6 @@ | |||
<!-- <module>demo-uflo</module>--> | |||
<!-- <module>demo-urule</module>--> | |||
<!-- <module>demo-activiti</module>--> | |||
<!-- <module>demo-async</module>--> | |||
<!-- <module>demo-dubbo</module>--> | |||
<!-- <module>demo-war</module>--> | |||
<!-- <module>demo-elasticsearch</module>--> | |||