From fd635d96b35c0cdd2e58f7ba3b7e321f31eb44d3 Mon Sep 17 00:00:00 2001 From: zhaiyongchao Date: Thu, 22 Mar 2018 16:37:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8@Async=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E5=BC=82=E6=AD=A5=E8=B0=83=E7=94=A8=EF=BC=9A=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E7=BA=BF=E7=A8=8B=E6=B1=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Chapter4-1-3/pom.xml | 56 +++++++++++++++++++ .../main/java/com/didispace/Application.java | 38 +++++++++++++ .../main/java/com/didispace/async/Task.java | 48 ++++++++++++++++ .../src/main/resources/application.properties | 0 .../java/com/didispace/ApplicationTests.java | 28 ++++++++++ 5 files changed, 170 insertions(+) create mode 100644 Chapter4-1-3/pom.xml create mode 100644 Chapter4-1-3/src/main/java/com/didispace/Application.java create mode 100644 Chapter4-1-3/src/main/java/com/didispace/async/Task.java create mode 100644 Chapter4-1-3/src/main/resources/application.properties create mode 100644 Chapter4-1-3/src/test/java/com/didispace/ApplicationTests.java diff --git a/Chapter4-1-3/pom.xml b/Chapter4-1-3/pom.xml new file mode 100644 index 0000000..b72b780 --- /dev/null +++ b/Chapter4-1-3/pom.xml @@ -0,0 +1,56 @@ + + + 4.0.0 + + com.didispace + Chapter4-1-3 + 1.0.0 + jar + + Chapter4-1-3 + Spring Boot project + + + org.springframework.boot + spring-boot-starter-parent + 1.5.10.RELEASE + + + + + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.projectlombok + lombok + 1.16.20 + provided + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/Chapter4-1-3/src/main/java/com/didispace/Application.java b/Chapter4-1-3/src/main/java/com/didispace/Application.java new file mode 100644 index 0000000..84d84ef --- /dev/null +++ b/Chapter4-1-3/src/main/java/com/didispace/Application.java @@ -0,0 +1,38 @@ +package com.didispace; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +import java.util.concurrent.Executor; +import java.util.concurrent.ThreadPoolExecutor; + + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + + @EnableAsync + @Configuration + class TaskPoolConfig { + + @Bean("taskExecutor") + public Executor taskExecutor() { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + executor.setCorePoolSize(10); + executor.setMaxPoolSize(20); + executor.setQueueCapacity(200); + executor.setKeepAliveSeconds(60); + executor.setThreadNamePrefix("taskExecutor-"); + executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); + return executor; + } + } + +} diff --git a/Chapter4-1-3/src/main/java/com/didispace/async/Task.java b/Chapter4-1-3/src/main/java/com/didispace/async/Task.java new file mode 100644 index 0000000..07891f8 --- /dev/null +++ b/Chapter4-1-3/src/main/java/com/didispace/async/Task.java @@ -0,0 +1,48 @@ +package com.didispace.async; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; + +import java.util.Random; + +/** + * @author 程序猿DD + * @version 1.0.0 + * @date 16/5/16 下午12:58. + * @blog http://blog.didispace.com + */ +@Slf4j +@Component +public class Task { + + public static Random random = new Random(); + + @Async("taskExecutor") + public void doTaskOne() throws Exception { + log.info("开始做任务一"); + long start = System.currentTimeMillis(); + Thread.sleep(random.nextInt(10000)); + long end = System.currentTimeMillis(); + log.info("完成任务一,耗时:" + (end - start) + "毫秒"); + } + + @Async("taskExecutor") + public void doTaskTwo() throws Exception { + log.info("开始做任务二"); + long start = System.currentTimeMillis(); + Thread.sleep(random.nextInt(10000)); + long end = System.currentTimeMillis(); + log.info("完成任务二,耗时:" + (end - start) + "毫秒"); + } + + @Async("taskExecutor") + public void doTaskThree() throws Exception { + log.info("开始做任务三"); + long start = System.currentTimeMillis(); + Thread.sleep(random.nextInt(10000)); + long end = System.currentTimeMillis(); + log.info("完成任务三,耗时:" + (end - start) + "毫秒"); + } + +} diff --git a/Chapter4-1-3/src/main/resources/application.properties b/Chapter4-1-3/src/main/resources/application.properties new file mode 100644 index 0000000..e69de29 diff --git a/Chapter4-1-3/src/test/java/com/didispace/ApplicationTests.java b/Chapter4-1-3/src/test/java/com/didispace/ApplicationTests.java new file mode 100644 index 0000000..03d3fab --- /dev/null +++ b/Chapter4-1-3/src/test/java/com/didispace/ApplicationTests.java @@ -0,0 +1,28 @@ +package com.didispace; + +import com.didispace.async.Task; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest +public class ApplicationTests { + + @Autowired + private Task task; + + @Test + public void test() throws Exception { + + task.doTaskOne(); + task.doTaskTwo(); + task.doTaskThree(); + + Thread.currentThread().join(); + } + +}