Skip to content

Commit 7866b41

Browse files
committed
如何隔离@async异步任务的线程池
1 parent 8547205 commit 7866b41

File tree

8 files changed

+52
-78
lines changed

8 files changed

+52
-78
lines changed

2.x/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494

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

9999
### 常见问题
100100

2.x/README_zh.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696

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

101101
### 常见问题
102102

2.x/chapter7-7/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
</parent>
1212

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

1818
<properties>
1919
<java.version>1.8</java.version>

2.x/chapter7-7/src/main/java/com/didispace/chapter77/AsyncTasks.java

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,31 @@
66

77
import java.util.Random;
88
import java.util.concurrent.CompletableFuture;
9-
import java.util.concurrent.Future;
109

1110
@Slf4j
1211
@Component
1312
public class AsyncTasks {
1413

1514
public static Random random = new Random();
1615

17-
@Async
18-
public CompletableFuture<String> doTaskOne() throws Exception {
19-
log.info("开始做任务一");
16+
@Async("taskExecutor1")
17+
public CompletableFuture<String> doTaskOne(String taskNo) throws Exception {
18+
log.info("开始任务:{}", taskNo);
2019
long start = System.currentTimeMillis();
2120
Thread.sleep(random.nextInt(10000));
2221
long end = System.currentTimeMillis();
23-
log.info("完成任务一,耗时:" + (end - start) + "毫秒");
24-
return CompletableFuture.completedFuture("任务一完成");
25-
}
26-
27-
@Async
28-
public CompletableFuture<String> doTaskTwo() throws Exception {
29-
log.info("开始做任务二");
30-
long start = System.currentTimeMillis();
31-
Thread.sleep(random.nextInt(10000));
32-
long end = System.currentTimeMillis();
33-
log.info("完成任务二,耗时:" + (end - start) + "毫秒");
34-
return CompletableFuture.completedFuture("任务二完成");
35-
}
36-
37-
@Async
38-
public CompletableFuture<String> doTaskThree() throws Exception {
39-
log.info("开始做任务三");
40-
long start = System.currentTimeMillis();
41-
Thread.sleep(random.nextInt(10000));
42-
long end = System.currentTimeMillis();
43-
log.info("完成任务三,耗时:" + (end - start) + "毫秒");
44-
return CompletableFuture.completedFuture("任务三完成");
22+
log.info("完成任务:{},耗时:{} 毫秒", taskNo, end - start);
23+
return CompletableFuture.completedFuture("任务完成");
4524
}
4625

4726
@Async("taskExecutor2")
48-
public CompletableFuture<String> doTaskFour() throws Exception {
49-
log.info("开始做任务四");
27+
public CompletableFuture<String> doTaskTwo(String taskNo) throws Exception {
28+
log.info("开始任务:{}", taskNo);
5029
long start = System.currentTimeMillis();
5130
Thread.sleep(random.nextInt(10000));
5231
long end = System.currentTimeMillis();
53-
log.info("完成任务四,耗时:" + (end - start) + "毫秒");
54-
return CompletableFuture.completedFuture("任务四完成");
32+
log.info("完成任务:{},耗时:{} 毫秒", taskNo, end - start);
33+
return CompletableFuture.completedFuture("任务完成");
5534
}
5635

5736
}

2.x/chapter7-7/src/main/java/com/didispace/chapter77/Chapter77Application.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,39 @@
1616

1717
@EnableAsync
1818
@SpringBootApplication
19-
public class Chapter76Application {
19+
public class Chapter77Application {
2020

2121
public static void main(String[] args) {
22-
SpringApplication.run(Chapter76Application.class, args);
22+
SpringApplication.run(Chapter77Application.class, args);
2323
}
2424

2525
@EnableAsync
2626
@Configuration
2727
class TaskPoolConfig {
2828

29-
// @Bean
30-
// public Executor taskExecutor2() {
31-
// ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
32-
// executor.setCorePoolSize(1);
33-
// executor.setMaxPoolSize(2);
34-
// executor.setQueueCapacity(50);
35-
// executor.setKeepAliveSeconds(60);
36-
// executor.setThreadNamePrefix("taskExecutor2-");
37-
// executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
38-
// return executor;
39-
// }
29+
@Bean
30+
public Executor taskExecutor1() {
31+
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
32+
executor.setCorePoolSize(2);
33+
executor.setMaxPoolSize(2);
34+
executor.setQueueCapacity(10);
35+
executor.setKeepAliveSeconds(60);
36+
executor.setThreadNamePrefix("executor-1-");
37+
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
38+
return executor;
39+
}
40+
41+
@Bean
42+
public Executor taskExecutor2() {
43+
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
44+
executor.setCorePoolSize(2);
45+
executor.setMaxPoolSize(2);
46+
executor.setQueueCapacity(10);
47+
executor.setKeepAliveSeconds(60);
48+
executor.setThreadNamePrefix("executor-2-");
49+
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
50+
return executor;
51+
}
4052

4153
}
4254

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +0,0 @@
1-
spring.task.execution.pool.core-size=2
2-
spring.task.execution.pool.max-size=5
3-
spring.task.execution.pool.queue-capacity=10
4-
spring.task.execution.pool.keep-alive=60s
5-
spring.task.execution.pool.allow-core-thread-timeout=true
6-
spring.task.execution.thread-name-prefix=task-
7-
8-
spring.task.execution.shutdown.await-termination=false
9-
spring.task.execution.shutdown.await-termination-period=30s

2.x/chapter7-7/src/test/java/com/didispace/chapter77/Chapter77ApplicationTests.java

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.didispace.chapter76;
1+
package com.didispace.chapter77;
22

33
import lombok.extern.slf4j.Slf4j;
44
import org.junit.jupiter.api.Test;
@@ -10,35 +10,27 @@
1010

1111
@Slf4j
1212
@SpringBootTest
13-
public class Chapter76ApplicationTests {
13+
public class Chapter77ApplicationTests {
1414

1515
@Autowired
1616
private AsyncTasks asyncTasks;
1717

1818
@Test
19-
public void test1() throws Exception {
19+
public void test() throws Exception {
2020
long start = System.currentTimeMillis();
2121

22-
CompletableFuture<String> task1 = asyncTasks.doTaskOne();
23-
CompletableFuture<String> task2 = asyncTasks.doTaskTwo();
24-
CompletableFuture<String> task3 = asyncTasks.doTaskThree();
22+
// 线程池1
23+
CompletableFuture<String> task1 = asyncTasks.doTaskOne("1");
24+
CompletableFuture<String> task2 = asyncTasks.doTaskOne("2");
25+
CompletableFuture<String> task3 = asyncTasks.doTaskOne("3");
2526

26-
CompletableFuture.allOf(task1, task2, task3).join();
27+
// 线程池2
28+
CompletableFuture<String> task4 = asyncTasks.doTaskTwo("4");
29+
CompletableFuture<String> task5 = asyncTasks.doTaskTwo("5");
30+
CompletableFuture<String> task6 = asyncTasks.doTaskTwo("6");
2731

28-
long end = System.currentTimeMillis();
29-
30-
log.info("任务全部完成,总耗时:" + (end - start) + "毫秒");
31-
}
32-
33-
@Test
34-
public void test2() throws Exception {
35-
long start = System.currentTimeMillis();
36-
37-
CompletableFuture<String> task1 = asyncTasks.doTaskFour();
38-
CompletableFuture<String> task2 = asyncTasks.doTaskFour();
39-
CompletableFuture<String> task3 = asyncTasks.doTaskFour();
40-
41-
CompletableFuture.allOf(task1, task2, task3).join();
32+
// 一起执行
33+
CompletableFuture.allOf(task1, task2, task3, task4, task5, task6).join();
4234

4335
long end = System.currentTimeMillis();
4436

2.x/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
<module>chapter7-4</module> <!-- 7-4 使用Elastic Job的namespace防止任务名冲突 -->
7878
<module>chapter7-5</module> <!-- 7-5 使用@Async实现异步任务 -->
7979
<module>chapter7-6</module> <!-- 7-6 配置@Async异步任务的线程池 -->
80-
<!--7-7 @Async异步任务的线程池隔离-->
80+
<module>chapter7-7</module> <!-- 7-7 如何隔离@Async异步任务的线程池-->
8181

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

0 commit comments

Comments
 (0)