Skip to content

Commit

Permalink
如何隔离@async异步任务的线程池
Browse files Browse the repository at this point in the history
  • Loading branch information
dyc87112 committed Sep 15, 2021
1 parent 8547205 commit 7866b41
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 78 deletions.
2 changes: 1 addition & 1 deletion 2.x/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@

- [Spring Boot 2.x基础教程:使用@Async实现异步任务](https://blog.didispace.com/spring-boot-learning-2-7-5)
- [Spring Boot 2.x基础教程:配置@Async异步任务的线程池](https://blog.didispace.com/spring-boot-learning-2-7-6)
- [Spring Boot 2.x基础教程:@Async异步任务的线程池隔离](https://blog.didispace.com/spring-boot-learning-2-7-7)
- [Spring Boot 2.x基础教程:如何隔离@Async异步任务的线程池](https://blog.didispace.com/spring-boot-learning-2-7-7)

### 常见问题

Expand Down
2 changes: 1 addition & 1 deletion 2.x/README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@

- [Spring Boot 2.x基础教程:使用@Async实现异步任务](https://blog.didispace.com/spring-boot-learning-2-7-5)
- [Spring Boot 2.x基础教程:配置@Async异步任务的线程池](https://blog.didispace.com/spring-boot-learning-2-7-6)
- [Spring Boot 2.x基础教程:@Async异步任务的线程池隔离](https://blog.didispace.com/spring-boot-learning-2-7-7)
- [Spring Boot 2.x基础教程:如何隔离@Async异步任务的线程池](https://blog.didispace.com/spring-boot-learning-2-7-7)

### 常见问题

Expand Down
4 changes: 2 additions & 2 deletions 2.x/chapter7-7/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
</parent>

<groupId>com.didispace</groupId>
<artifactId>chapter7-6</artifactId>
<artifactId>chapter7-7</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>@Async异步任务的线程池隔离</description>
<description>如何隔离@Async异步任务的线程池</description>

<properties>
<java.version>1.8</java.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,31 @@

import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;

@Slf4j
@Component
public class AsyncTasks {

public static Random random = new Random();

@Async
public CompletableFuture<String> doTaskOne() throws Exception {
log.info("开始做任务一");
@Async("taskExecutor1")
public CompletableFuture<String> doTaskOne(String taskNo) throws Exception {
log.info("开始任务:{}", taskNo);
long start = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
long end = System.currentTimeMillis();
log.info("完成任务一,耗时:" + (end - start) + "毫秒");
return CompletableFuture.completedFuture("任务一完成");
}

@Async
public CompletableFuture<String> doTaskTwo() throws Exception {
log.info("开始做任务二");
long start = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
long end = System.currentTimeMillis();
log.info("完成任务二,耗时:" + (end - start) + "毫秒");
return CompletableFuture.completedFuture("任务二完成");
}

@Async
public CompletableFuture<String> doTaskThree() throws Exception {
log.info("开始做任务三");
long start = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
long end = System.currentTimeMillis();
log.info("完成任务三,耗时:" + (end - start) + "毫秒");
return CompletableFuture.completedFuture("任务三完成");
log.info("完成任务:{},耗时:{} 毫秒", taskNo, end - start);
return CompletableFuture.completedFuture("任务完成");
}

@Async("taskExecutor2")
public CompletableFuture<String> doTaskFour() throws Exception {
log.info("开始做任务四");
public CompletableFuture<String> doTaskTwo(String taskNo) throws Exception {
log.info("开始任务:{}", taskNo);
long start = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
long end = System.currentTimeMillis();
log.info("完成任务四,耗时:" + (end - start) + "毫秒");
return CompletableFuture.completedFuture("任务四完成");
log.info("完成任务:{},耗时:{} 毫秒", taskNo, end - start);
return CompletableFuture.completedFuture("任务完成");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,39 @@

@EnableAsync
@SpringBootApplication
public class Chapter76Application {
public class Chapter77Application {

public static void main(String[] args) {
SpringApplication.run(Chapter76Application.class, args);
SpringApplication.run(Chapter77Application.class, args);
}

@EnableAsync
@Configuration
class TaskPoolConfig {

// @Bean
// public Executor taskExecutor2() {
// ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// executor.setCorePoolSize(1);
// executor.setMaxPoolSize(2);
// executor.setQueueCapacity(50);
// executor.setKeepAliveSeconds(60);
// executor.setThreadNamePrefix("taskExecutor2-");
// executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// return executor;
// }
@Bean
public Executor taskExecutor1() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2);
executor.setMaxPoolSize(2);
executor.setQueueCapacity(10);
executor.setKeepAliveSeconds(60);
executor.setThreadNamePrefix("executor-1-");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return executor;
}

@Bean
public Executor taskExecutor2() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2);
executor.setMaxPoolSize(2);
executor.setQueueCapacity(10);
executor.setKeepAliveSeconds(60);
executor.setThreadNamePrefix("executor-2-");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return executor;
}

}

Expand Down
9 changes: 0 additions & 9 deletions 2.x/chapter7-7/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,9 +0,0 @@
spring.task.execution.pool.core-size=2
spring.task.execution.pool.max-size=5
spring.task.execution.pool.queue-capacity=10
spring.task.execution.pool.keep-alive=60s
spring.task.execution.pool.allow-core-thread-timeout=true
spring.task.execution.thread-name-prefix=task-

spring.task.execution.shutdown.await-termination=false
spring.task.execution.shutdown.await-termination-period=30s
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.didispace.chapter76;
package com.didispace.chapter77;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
Expand All @@ -10,35 +10,27 @@

@Slf4j
@SpringBootTest
public class Chapter76ApplicationTests {
public class Chapter77ApplicationTests {

@Autowired
private AsyncTasks asyncTasks;

@Test
public void test1() throws Exception {
public void test() throws Exception {
long start = System.currentTimeMillis();

CompletableFuture<String> task1 = asyncTasks.doTaskOne();
CompletableFuture<String> task2 = asyncTasks.doTaskTwo();
CompletableFuture<String> task3 = asyncTasks.doTaskThree();
// 线程池1
CompletableFuture<String> task1 = asyncTasks.doTaskOne("1");
CompletableFuture<String> task2 = asyncTasks.doTaskOne("2");
CompletableFuture<String> task3 = asyncTasks.doTaskOne("3");

CompletableFuture.allOf(task1, task2, task3).join();
// 线程池2
CompletableFuture<String> task4 = asyncTasks.doTaskTwo("4");
CompletableFuture<String> task5 = asyncTasks.doTaskTwo("5");
CompletableFuture<String> task6 = asyncTasks.doTaskTwo("6");

long end = System.currentTimeMillis();

log.info("任务全部完成,总耗时:" + (end - start) + "毫秒");
}

@Test
public void test2() throws Exception {
long start = System.currentTimeMillis();

CompletableFuture<String> task1 = asyncTasks.doTaskFour();
CompletableFuture<String> task2 = asyncTasks.doTaskFour();
CompletableFuture<String> task3 = asyncTasks.doTaskFour();

CompletableFuture.allOf(task1, task2, task3).join();
// 一起执行
CompletableFuture.allOf(task1, task2, task3, task4, task5, task6).join();

long end = System.currentTimeMillis();

Expand Down
2 changes: 1 addition & 1 deletion 2.x/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
<module>chapter7-4</module> <!-- 7-4 使用Elastic Job的namespace防止任务名冲突 -->
<module>chapter7-5</module> <!-- 7-5 使用@Async实现异步任务 -->
<module>chapter7-6</module> <!-- 7-6 配置@Async异步任务的线程池 -->
<!--7-7 @Async异步任务的线程池隔离-->
<module>chapter7-7</module> <!-- 7-7 如何隔离@Async异步任务的线程池-->

<!-- 安全控制 -->
<module>chapter8-1</module> <!-- Spring Security快速入门 -->
Expand Down

0 comments on commit 7866b41

Please sign in to comment.