diff --git a/spring-boot-demo-async/pom.xml b/spring-boot-demo-async/pom.xml index 1013ba6..c35e221 100644 --- a/spring-boot-demo-async/pom.xml +++ b/spring-boot-demo-async/pom.xml @@ -33,6 +33,12 @@ spring-boot-starter-test test + + + org.projectlombok + lombok + true + diff --git a/spring-boot-demo-async/src/main/java/com/xkcoding/async/SpringBootDemoAsyncApplication.java b/spring-boot-demo-async/src/main/java/com/xkcoding/async/SpringBootDemoAsyncApplication.java index 2649456..10eaa4a 100644 --- a/spring-boot-demo-async/src/main/java/com/xkcoding/async/SpringBootDemoAsyncApplication.java +++ b/spring-boot-demo-async/src/main/java/com/xkcoding/async/SpringBootDemoAsyncApplication.java @@ -2,7 +2,22 @@ package com.xkcoding.async; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableAsync; +/** + *

+ * 启动器 + *

+ * + * @package: com.xkcoding.async + * @description: 启动器 + * @author: yangkai.shen + * @date: Created in 2018-12-29 10:28 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@EnableAsync @SpringBootApplication public class SpringBootDemoAsyncApplication { diff --git a/spring-boot-demo-async/src/main/java/com/xkcoding/async/task/TaskFactory.java b/spring-boot-demo-async/src/main/java/com/xkcoding/async/task/TaskFactory.java new file mode 100644 index 0000000..1cd30a7 --- /dev/null +++ b/spring-boot-demo-async/src/main/java/com/xkcoding/async/task/TaskFactory.java @@ -0,0 +1,81 @@ +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.TimeUnit; + +/** + *

+ * 任务工厂 + *

+ * + * @package: com.xkcoding.async.task + * @description: 任务工厂 + * @author: yangkai.shen + * @date: Created in 2018-12-29 10:37 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@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()); + } +} diff --git a/spring-boot-demo-async/src/main/resources/application.yml b/spring-boot-demo-async/src/main/resources/application.yml new file mode 100644 index 0000000..21b50ce --- /dev/null +++ b/spring-boot-demo-async/src/main/resources/application.yml @@ -0,0 +1,16 @@ +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- \ No newline at end of file diff --git a/spring-boot-demo-async/src/test/java/com/xkcoding/async/task/TaskFactoryTest.java b/spring-boot-demo-async/src/test/java/com/xkcoding/async/task/TaskFactoryTest.java new file mode 100644 index 0000000..326807b --- /dev/null +++ b/spring-boot-demo-async/src/test/java/com/xkcoding/async/task/TaskFactoryTest.java @@ -0,0 +1,61 @@ +package com.xkcoding.async.task; + +import com.xkcoding.async.SpringBootDemoAsyncApplicationTests; +import lombok.extern.slf4j.Slf4j; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; + +/** + *

+ * 测试任务 + *

+ * + * @package: com.xkcoding.async.task + * @description: 测试任务 + * @author: yangkai.shen + * @date: Created in 2018-12-29 10:49 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@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)); + } +} \ No newline at end of file