Skip to content

Commit ee78526

Browse files
committed
@async异步任务的线程池配置
1 parent 679ebf6 commit ee78526

File tree

6 files changed

+145
-1
lines changed

6 files changed

+145
-1
lines changed

2.x/chapter7-6/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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.didispace.chapter76;
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
17+
public CompletableFuture<String> doTaskOne() throws Exception {
18+
log.info("开始做任务一");
19+
long start = System.currentTimeMillis();
20+
Thread.sleep(random.nextInt(10000));
21+
long end = System.currentTimeMillis();
22+
log.info("完成任务一,耗时:" + (end - start) + "毫秒");
23+
return CompletableFuture.completedFuture("任务一完成");
24+
}
25+
26+
@Async
27+
public CompletableFuture<String> doTaskTwo() throws Exception {
28+
log.info("开始做任务二");
29+
long start = System.currentTimeMillis();
30+
Thread.sleep(random.nextInt(10000));
31+
long end = System.currentTimeMillis();
32+
log.info("完成任务二,耗时:" + (end - start) + "毫秒");
33+
return CompletableFuture.completedFuture("任务二完成");
34+
}
35+
36+
@Async
37+
public CompletableFuture<String> doTaskThree() throws Exception {
38+
log.info("开始做任务三");
39+
long start = System.currentTimeMillis();
40+
Thread.sleep(random.nextInt(10000));
41+
long end = System.currentTimeMillis();
42+
log.info("完成任务三,耗时:" + (end - start) + "毫秒");
43+
return CompletableFuture.completedFuture("任务三完成");
44+
}
45+
46+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.didispace.chapter76;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.scheduling.annotation.EnableAsync;
6+
7+
@EnableAsync
8+
@SpringBootApplication
9+
public class Chapter76Application {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(Chapter76Application.class, args);
13+
}
14+
15+
}

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

Whitespace-only changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 test() 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+
}

2.x/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
<module>chapter7-3</module> <!-- 7-3 使用Elastic Job的分片配置 -->
7777
<module>chapter7-4</module> <!-- 7-4 使用Elastic Job的namespace防止任务名冲突 -->
7878
<module>chapter7-5</module> <!-- 7-5 使用@Async实现异步任务 -->
79-
79+
<module>chapter7-6</module> <!-- 7-6 @Async异步任务的线程池配置 -->
8080

8181
</modules>
8282
</project>

0 commit comments

Comments
 (0)