Skip to content

Commit 9feeec7

Browse files
committed
@async异步任务线程池配置拒绝策略
1 parent 7866b41 commit 9feeec7

File tree

8 files changed

+197
-1
lines changed

8 files changed

+197
-1
lines changed

2.x/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
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)
9797
- [Spring Boot 2.x基础教程:如何隔离@Async异步任务的线程池](https://blog.didispace.com/spring-boot-learning-2-7-7)
98+
- [Spring Boot 2.x基础教程:为@Async异步任务线程池配置拒绝策略](https://blog.didispace.com/spring-boot-learning-2-7-8)
9899

99100
### 常见问题
100101

2.x/README_zh.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
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)
9999
- [Spring Boot 2.x基础教程:如何隔离@Async异步任务的线程池](https://blog.didispace.com/spring-boot-learning-2-7-7)
100+
- [Spring Boot 2.x基础教程:为@Async异步任务的线程池设置拒绝策略](https://blog.didispace.com/spring-boot-learning-2-7-8)
100101

101102
### 常见问题
102103

2.x/chapter7-8/pom.xml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>2.5.1</version>
10+
<relativePath/> <!-- lookup parent from repository -->
11+
</parent>
12+
13+
<groupId>com.didispace</groupId>
14+
<artifactId>chapter7-8</artifactId>
15+
<version>0.0.1-SNAPSHOT</version>
16+
<description>为@Async异步任务线程池配置拒绝策略</description>
17+
18+
<properties>
19+
<java.version>1.8</java.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-web</artifactId>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>org.projectlombok</groupId>
30+
<artifactId>lombok</artifactId>
31+
<scope>provided</scope>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-test</artifactId>
37+
<scope>test</scope>
38+
</dependency>
39+
</dependencies>
40+
41+
<build>
42+
<plugins>
43+
<plugin>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-maven-plugin</artifactId>
46+
</plugin>
47+
</plugins>
48+
</build>
49+
50+
</project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.didispace.chapter78;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.scheduling.annotation.Async;
5+
import org.springframework.stereotype.Component;
6+
7+
import java.util.Random;
8+
import java.util.concurrent.CompletableFuture;
9+
10+
@Slf4j
11+
@Component
12+
public class AsyncTasks {
13+
14+
public static Random random = new Random();
15+
16+
@Async("taskExecutor1")
17+
public CompletableFuture<String> doTaskOne(String taskNo) throws Exception {
18+
log.info("开始任务:{}", taskNo);
19+
long start = System.currentTimeMillis();
20+
Thread.sleep(random.nextInt(10000));
21+
long end = System.currentTimeMillis();
22+
log.info("完成任务:{},耗时:{} 毫秒", taskNo, end - start);
23+
return CompletableFuture.completedFuture("任务完成");
24+
}
25+
26+
@Async("taskExecutor2")
27+
public CompletableFuture<String> doTaskTwo(String taskNo) throws Exception {
28+
log.info("开始任务:{}", taskNo);
29+
long start = System.currentTimeMillis();
30+
Thread.sleep(random.nextInt(10000));
31+
long end = System.currentTimeMillis();
32+
log.info("完成任务:{},耗时:{} 毫秒", taskNo, end - start);
33+
return CompletableFuture.completedFuture("任务完成");
34+
}
35+
36+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.didispace.chapter78;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.scheduling.annotation.EnableAsync;
9+
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
10+
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.RestController;
13+
14+
import java.util.concurrent.Executor;
15+
import java.util.concurrent.ThreadPoolExecutor;
16+
17+
@EnableAsync
18+
@SpringBootApplication
19+
public class Chapter78Application {
20+
21+
public static void main(String[] args) {
22+
SpringApplication.run(Chapter78Application.class, args);
23+
}
24+
25+
@EnableAsync
26+
@Configuration
27+
class TaskPoolConfig {
28+
29+
@Bean
30+
public Executor taskExecutor1() {
31+
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
32+
executor.setCorePoolSize(2);
33+
executor.setMaxPoolSize(2);
34+
executor.setQueueCapacity(2);
35+
executor.setKeepAliveSeconds(60);
36+
executor.setThreadNamePrefix("executor-1-");
37+
38+
// 配置拒绝策略
39+
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
40+
return executor;
41+
}
42+
43+
}
44+
45+
}

2.x/chapter7-8/src/main/resources/application.properties

Whitespace-only changes.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.didispace.chapter78;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
8+
import java.util.concurrent.CompletableFuture;
9+
import java.util.concurrent.Future;
10+
11+
@Slf4j
12+
@SpringBootTest
13+
public class Chapter78ApplicationTests {
14+
15+
@Autowired
16+
private AsyncTasks asyncTasks;
17+
18+
@Test
19+
public void test() throws Exception {
20+
// 线程池配置:core-2,max-2,queue=2,可以容纳4个任务提交
21+
22+
long start = System.currentTimeMillis();
23+
24+
// 线程池1
25+
CompletableFuture<String> task1 = asyncTasks.doTaskOne("1");
26+
CompletableFuture<String> task2 = asyncTasks.doTaskOne("2");
27+
CompletableFuture<String> task3 = asyncTasks.doTaskOne("3");
28+
CompletableFuture<String> task4 = asyncTasks.doTaskOne("4");
29+
30+
// 一起执行
31+
CompletableFuture.allOf(task1, task2, task3, task4).join();
32+
33+
long end = System.currentTimeMillis();
34+
35+
log.info("任务全部完成,总耗时:" + (end - start) + "毫秒");
36+
}
37+
38+
@Test
39+
public void test2() throws Exception {
40+
// 线程池配置:core-2,max-2,queue=2,同时有5个任务,出现下面异常:
41+
// org.springframework.core.task.TaskRejectedException: Executor [java.util.concurrent.ThreadPoolExecutor@59901c4d[Running, pool size = 2,
42+
// active threads = 0, queued tasks = 2, completed tasks = 4]] did not accept task: java.util.concurrent.CompletableFuture$AsyncSupply@408e96d9
43+
44+
long start = System.currentTimeMillis();
45+
46+
// 线程池1
47+
CompletableFuture<String> task1 = asyncTasks.doTaskOne("1");
48+
CompletableFuture<String> task2 = asyncTasks.doTaskOne("2");
49+
CompletableFuture<String> task3 = asyncTasks.doTaskOne("3");
50+
CompletableFuture<String> task4 = asyncTasks.doTaskOne("4");
51+
CompletableFuture<String> task5 = asyncTasks.doTaskOne("5");
52+
53+
// 一起执行
54+
CompletableFuture.allOf(task1, task2, task3, task4, task5).join();
55+
56+
long end = System.currentTimeMillis();
57+
58+
log.info("任务全部完成,总耗时:" + (end - start) + "毫秒");
59+
}
60+
61+
}

2.x/pom.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@
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-
<module>chapter7-7</module> <!-- 7-7 如何隔离@Async异步任务的线程池-->
80+
<module>chapter7-7</module> <!-- 7-7 如何隔离@Async异步任务的线程池 -->
81+
<module>chapter7-8</module> <!-- 7-8 为@Async异步任务线程池配置拒绝策略 -->
82+
<!-- 7-9 为@Async异步任务添加超时时间 -->
8183

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

0 commit comments

Comments
 (0)