From 650e792dfb2931aec871eb20881093bfe92c90d1 Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819@qq.com> Date: Fri, 19 Aug 2022 22:06:57 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E5=9F=BA=E7=A1=80=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E4=B9=8B=20=E5=BC=82=E6=AD=A5=E4=BB=BB=E5=8A=A1=20?= =?UTF-8?q?=E6=A1=88=E4=BE=8B=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo-async/.gitignore | 25 -- demo-async/README.md | 257 --------------------- demo-async/pom.xml | 54 ----- .../async/SpringBootDemoAsyncApplicationTests.java | 17 -- demo-base/demo-base-async/README.md | 209 +++++++++++++++++ demo-base/demo-base-async/pom.xml | 52 +++++ .../java/com/xkcoding/async/AsyncApplication.java | 6 +- .../com/xkcoding/async/task/MockTaskFactory.java | 21 +- .../src/main/resources/application.yml | 0 .../com/xkcoding/async/AsyncApplicationTests.java | 14 ++ .../xkcoding/async/task/MockTaskFactoryTest.java | 30 +-- demo-base/pom.xml | 1 + pom.xml | 1 - 13 files changed, 306 insertions(+), 381 deletions(-) delete mode 100644 demo-async/.gitignore delete mode 100644 demo-async/README.md delete mode 100644 demo-async/pom.xml delete mode 100644 demo-async/src/test/java/com/xkcoding/async/SpringBootDemoAsyncApplicationTests.java create mode 100644 demo-base/demo-base-async/README.md create mode 100644 demo-base/demo-base-async/pom.xml rename demo-async/src/main/java/com/xkcoding/async/SpringBootDemoAsyncApplication.java => demo-base/demo-base-async/src/main/java/com/xkcoding/async/AsyncApplication.java (69%) rename demo-async/src/main/java/com/xkcoding/async/task/TaskFactory.java => demo-base/demo-base-async/src/main/java/com/xkcoding/async/task/MockTaskFactory.java (70%) rename {demo-async => demo-base/demo-base-async}/src/main/resources/application.yml (100%) create mode 100644 demo-base/demo-base-async/src/test/java/com/xkcoding/async/AsyncApplicationTests.java rename demo-async/src/test/java/com/xkcoding/async/task/TaskFactoryTest.java => demo-base/demo-base-async/src/test/java/com/xkcoding/async/task/MockTaskFactoryTest.java (56%) diff --git a/demo-async/.gitignore b/demo-async/.gitignore deleted file mode 100644 index 82eca33..0000000 --- a/demo-async/.gitignore +++ /dev/null @@ -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/ \ No newline at end of file diff --git a/demo-async/README.md b/demo-async/README.md deleted file mode 100644 index 4561544..0000000 --- a/demo-async/README.md +++ /dev/null @@ -1,257 +0,0 @@ -# spring-boot-demo-async - -> 此 demo 主要演示了 Spring Boot 如何使用原生提供的异步任务支持,实现异步执行任务。 - -## pom.xml - -```xml - - - 4.0.0 - - spring-boot-demo-async - 1.0.0-SNAPSHOT - jar - - spring-boot-demo-async - Demo project for Spring Boot - - - com.xkcoding - spring-boot-demo - 1.0.0-SNAPSHOT - - - - UTF-8 - UTF-8 - 1.8 - - - - - org.springframework.boot - spring-boot-starter - - - - org.springframework.boot - spring-boot-starter-test - test - - - - org.projectlombok - lombok - true - - - - - spring-boot-demo-async - - - org.springframework.boot - spring-boot-maven-plugin - - - - - -``` - -## 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 -/** - *

- * 启动器 - *

- * - * @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 -/** - *

- * 任务工厂 - *

- * - * @author yangkai.shen - * @date Created in 2018-12-29 10:37 - */ -@Component -@Slf4j -public class TaskFactory { - - /** - * 模拟5秒的异步任务 - */ - @Async - public Future asyncTask1() throws InterruptedException { - doTask("asyncTask1", 5); - return new AsyncResult<>(Boolean.TRUE); - } - - /** - * 模拟2秒的异步任务 - */ - @Async - public Future asyncTask2() throws InterruptedException { - doTask("asyncTask2", 2); - return new AsyncResult<>(Boolean.TRUE); - } - - /** - * 模拟3秒的异步任务 - */ - @Async - public Future 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 -/** - *

- * 测试任务 - *

- * - * @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 asyncTask1 = task.asyncTask1(); - Future asyncTask2 = task.asyncTask2(); - Future 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 diff --git a/demo-async/pom.xml b/demo-async/pom.xml deleted file mode 100644 index 388e38b..0000000 --- a/demo-async/pom.xml +++ /dev/null @@ -1,54 +0,0 @@ - - - 4.0.0 - - demo-async - 1.0.0-SNAPSHOT - jar - - demo-async - Demo project for Spring Boot - - - com.xkcoding - spring-boot-demo - 1.0.0-SNAPSHOT - - - - UTF-8 - UTF-8 - 1.8 - - - - - org.springframework.boot - spring-boot-starter - - - - org.springframework.boot - spring-boot-starter-test - test - - - - org.projectlombok - lombok - true - - - - - demo-async - - - org.springframework.boot - spring-boot-maven-plugin - - - - - diff --git a/demo-async/src/test/java/com/xkcoding/async/SpringBootDemoAsyncApplicationTests.java b/demo-async/src/test/java/com/xkcoding/async/SpringBootDemoAsyncApplicationTests.java deleted file mode 100644 index 01278ee..0000000 --- a/demo-async/src/test/java/com/xkcoding/async/SpringBootDemoAsyncApplicationTests.java +++ /dev/null @@ -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() { - } - -} - diff --git a/demo-base/demo-base-async/README.md b/demo-base/demo-base-async/README.md new file mode 100644 index 0000000..9c181e8 --- /dev/null +++ b/demo-base/demo-base-async/README.md @@ -0,0 +1,209 @@ +## spring-boot-demo-async + +> 此 demo 主要演示了 Spring Boot 如何使用原生提供的异步任务支持,实现异步执行任务。 + +### 1.开发步骤 + +#### 1.1.添加依赖 + +```xml + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.projectlombok + lombok + true + + +``` + +#### 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 asyncTask1() throws InterruptedException { + doTask("asyncTask1", 5); + return CompletableFuture.completedFuture(Boolean.TRUE); + } + + /** + * 模拟2秒的异步任务 + */ + @Async + public CompletableFuture asyncTask2() throws InterruptedException { + doTask("asyncTask2", 2); + return CompletableFuture.completedFuture(Boolean.TRUE); + } + + /** + * 模拟3秒的异步任务 + */ + @Async + public CompletableFuture 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 asyncResult1 = task.asyncTask1(); + CompletableFuture asyncResult2 = task.asyncTask2(); + CompletableFuture asyncResult3 = task.asyncTask3(); + + CompletableFuture 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 diff --git a/demo-base/demo-base-async/pom.xml b/demo-base/demo-base-async/pom.xml new file mode 100644 index 0000000..4d660af --- /dev/null +++ b/demo-base/demo-base-async/pom.xml @@ -0,0 +1,52 @@ + + + + com.xkcoding + demo-base + 1.0.0-SNAPSHOT + + + 4.0.0 + + demo-base-async + 1.0.0-SNAPSHOT + jar + + demo-base-async + Demo project for Spring Boot + + + 17 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.projectlombok + lombok + true + + + + + demo-base-async + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/demo-async/src/main/java/com/xkcoding/async/SpringBootDemoAsyncApplication.java b/demo-base/demo-base-async/src/main/java/com/xkcoding/async/AsyncApplication.java similarity index 69% rename from demo-async/src/main/java/com/xkcoding/async/SpringBootDemoAsyncApplication.java rename to demo-base/demo-base-async/src/main/java/com/xkcoding/async/AsyncApplication.java index 6d1e8e8..0bf6bec 100644 --- a/demo-async/src/main/java/com/xkcoding/async/SpringBootDemoAsyncApplication.java +++ b/demo-base/demo-base-async/src/main/java/com/xkcoding/async/AsyncApplication.java @@ -10,14 +10,14 @@ import org.springframework.scheduling.annotation.EnableAsync; *

* * @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); } } diff --git a/demo-async/src/main/java/com/xkcoding/async/task/TaskFactory.java b/demo-base/demo-base-async/src/main/java/com/xkcoding/async/task/MockTaskFactory.java similarity index 70% rename from demo-async/src/main/java/com/xkcoding/async/task/TaskFactory.java rename to demo-base/demo-base-async/src/main/java/com/xkcoding/async/task/MockTaskFactory.java index e492110..dc7ddb5 100644 --- a/demo-async/src/main/java/com/xkcoding/async/task/TaskFactory.java +++ b/demo-base/demo-base-async/src/main/java/com/xkcoding/async/task/MockTaskFactory.java @@ -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; /** *

- * 任务工厂 + * 模拟任务工厂 *

* * @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 asyncTask1() throws InterruptedException { + public CompletableFuture asyncTask1() throws InterruptedException { doTask("asyncTask1", 5); - return new AsyncResult<>(Boolean.TRUE); + return CompletableFuture.completedFuture(Boolean.TRUE); } /** * 模拟2秒的异步任务 */ @Async - public Future asyncTask2() throws InterruptedException { + public CompletableFuture asyncTask2() throws InterruptedException { doTask("asyncTask2", 2); - return new AsyncResult<>(Boolean.TRUE); + return CompletableFuture.completedFuture(Boolean.TRUE); } /** * 模拟3秒的异步任务 */ @Async - public Future asyncTask3() throws InterruptedException { + public CompletableFuture asyncTask3() throws InterruptedException { doTask("asyncTask3", 3); - return new AsyncResult<>(Boolean.TRUE); + return CompletableFuture.completedFuture(Boolean.TRUE); } /** diff --git a/demo-async/src/main/resources/application.yml b/demo-base/demo-base-async/src/main/resources/application.yml similarity index 100% rename from demo-async/src/main/resources/application.yml rename to demo-base/demo-base-async/src/main/resources/application.yml diff --git a/demo-base/demo-base-async/src/test/java/com/xkcoding/async/AsyncApplicationTests.java b/demo-base/demo-base-async/src/test/java/com/xkcoding/async/AsyncApplicationTests.java new file mode 100644 index 0000000..234767e --- /dev/null +++ b/demo-base/demo-base-async/src/test/java/com/xkcoding/async/AsyncApplicationTests.java @@ -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() { + } + +} + diff --git a/demo-async/src/test/java/com/xkcoding/async/task/TaskFactoryTest.java b/demo-base/demo-base-async/src/test/java/com/xkcoding/async/task/MockTaskFactoryTest.java similarity index 56% rename from demo-async/src/test/java/com/xkcoding/async/task/TaskFactoryTest.java rename to demo-base/demo-base-async/src/test/java/com/xkcoding/async/task/MockTaskFactoryTest.java index 89a226f..8b7f584 100644 --- a/demo-async/src/test/java/com/xkcoding/async/task/TaskFactoryTest.java +++ b/demo-base/demo-base-async/src/test/java/com/xkcoding/async/task/MockTaskFactoryTest.java @@ -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; /** *

- * 测试任务 + * 异步任务单元测试 *

* * @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 asyncTask1 = task.asyncTask1(); - Future asyncTask2 = task.asyncTask2(); - Future asyncTask3 = task.asyncTask3(); + CompletableFuture asyncResult1 = task.asyncTask1(); + CompletableFuture asyncResult2 = task.asyncTask2(); + CompletableFuture asyncResult3 = task.asyncTask3(); + CompletableFuture 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(); diff --git a/demo-base/pom.xml b/demo-base/pom.xml index 40bc35b..93eaa4c 100644 --- a/demo-base/pom.xml +++ b/demo-base/pom.xml @@ -21,6 +21,7 @@ demo-base-helloworld demo-base-properties + demo-base-async diff --git a/pom.xml b/pom.xml index c47c5b0..9871805 100644 --- a/pom.xml +++ b/pom.xml @@ -65,7 +65,6 @@ -