Skip to content

Commit 2a5e47d

Browse files
committed
@async异步任务的线程池隔离
1 parent 0bfbf65 commit 2a5e47d

File tree

5 files changed

+207
-0
lines changed

5 files changed

+207
-0
lines changed

2.x/chapter7-7/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-6</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: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.didispace.chapter77;
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+
import java.util.concurrent.Future;
10+
11+
@Slf4j
12+
@Component
13+
public class AsyncTasks {
14+
15+
public static Random random = new Random();
16+
17+
@Async
18+
public CompletableFuture<String> doTaskOne() throws Exception {
19+
log.info("开始做任务一");
20+
long start = System.currentTimeMillis();
21+
Thread.sleep(random.nextInt(10000));
22+
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("任务三完成");
45+
}
46+
47+
@Async("taskExecutor2")
48+
public CompletableFuture<String> doTaskFour() throws Exception {
49+
log.info("开始做任务四");
50+
long start = System.currentTimeMillis();
51+
Thread.sleep(random.nextInt(10000));
52+
long end = System.currentTimeMillis();
53+
log.info("完成任务四,耗时:" + (end - start) + "毫秒");
54+
return CompletableFuture.completedFuture("任务四完成");
55+
}
56+
57+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.didispace.chapter77;
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 Chapter76Application {
20+
21+
public static void main(String[] args) {
22+
SpringApplication.run(Chapter76Application.class, args);
23+
}
24+
25+
@EnableAsync
26+
@Configuration
27+
class TaskPoolConfig {
28+
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+
// }
40+
41+
}
42+
43+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.didispace.chapter76;
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 Chapter76ApplicationTests {
14+
15+
@Autowired
16+
private AsyncTasks asyncTasks;
17+
18+
@Test
19+
public void test1() throws Exception {
20+
long start = System.currentTimeMillis();
21+
22+
CompletableFuture<String> task1 = asyncTasks.doTaskOne();
23+
CompletableFuture<String> task2 = asyncTasks.doTaskTwo();
24+
CompletableFuture<String> task3 = asyncTasks.doTaskThree();
25+
26+
CompletableFuture.allOf(task1, task2, task3).join();
27+
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();
42+
43+
long end = System.currentTimeMillis();
44+
45+
log.info("任务全部完成,总耗时:" + (end - start) + "毫秒");
46+
}
47+
48+
}

0 commit comments

Comments
 (0)