Skip to content

Commit fd635d9

Browse files
committed
使用@async实现异步调用:自定义线程池
1 parent 660c0c8 commit fd635d9

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed

Chapter4-1-3/pom.xml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
<groupId>com.didispace</groupId>
7+
<artifactId>Chapter4-1-3</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>Chapter4-1-3</name>
12+
<description>Spring Boot project</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.10.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<java.version>1.8</java.version>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter</artifactId>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-test</artifactId>
35+
<scope>test</scope>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.projectlombok</groupId>
40+
<artifactId>lombok</artifactId>
41+
<version>1.16.20</version>
42+
<scope>provided</scope>
43+
</dependency>
44+
45+
</dependencies>
46+
47+
<build>
48+
<plugins>
49+
<plugin>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-maven-plugin</artifactId>
52+
</plugin>
53+
</plugins>
54+
</build>
55+
56+
</project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.didispace;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.scheduling.annotation.EnableAsync;
8+
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
9+
10+
import java.util.concurrent.Executor;
11+
import java.util.concurrent.ThreadPoolExecutor;
12+
13+
14+
@SpringBootApplication
15+
public class Application {
16+
17+
public static void main(String[] args) {
18+
SpringApplication.run(Application.class, args);
19+
}
20+
21+
@EnableAsync
22+
@Configuration
23+
class TaskPoolConfig {
24+
25+
@Bean("taskExecutor")
26+
public Executor taskExecutor() {
27+
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
28+
executor.setCorePoolSize(10);
29+
executor.setMaxPoolSize(20);
30+
executor.setQueueCapacity(200);
31+
executor.setKeepAliveSeconds(60);
32+
executor.setThreadNamePrefix("taskExecutor-");
33+
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
34+
return executor;
35+
}
36+
}
37+
38+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.didispace.async;
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+
9+
/**
10+
* @author 程序猿DD
11+
* @version 1.0.0
12+
* @date 16/5/16 下午12:58.
13+
* @blog http://blog.didispace.com
14+
*/
15+
@Slf4j
16+
@Component
17+
public class Task {
18+
19+
public static Random random = new Random();
20+
21+
@Async("taskExecutor")
22+
public void doTaskOne() throws Exception {
23+
log.info("开始做任务一");
24+
long start = System.currentTimeMillis();
25+
Thread.sleep(random.nextInt(10000));
26+
long end = System.currentTimeMillis();
27+
log.info("完成任务一,耗时:" + (end - start) + "毫秒");
28+
}
29+
30+
@Async("taskExecutor")
31+
public void doTaskTwo() throws Exception {
32+
log.info("开始做任务二");
33+
long start = System.currentTimeMillis();
34+
Thread.sleep(random.nextInt(10000));
35+
long end = System.currentTimeMillis();
36+
log.info("完成任务二,耗时:" + (end - start) + "毫秒");
37+
}
38+
39+
@Async("taskExecutor")
40+
public void doTaskThree() throws Exception {
41+
log.info("开始做任务三");
42+
long start = System.currentTimeMillis();
43+
Thread.sleep(random.nextInt(10000));
44+
long end = System.currentTimeMillis();
45+
log.info("完成任务三,耗时:" + (end - start) + "毫秒");
46+
}
47+
48+
}

Chapter4-1-3/src/main/resources/application.properties

Whitespace-only changes.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.didispace;
2+
3+
import com.didispace.async.Task;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
9+
10+
11+
@RunWith(SpringJUnit4ClassRunner.class)
12+
@SpringBootTest
13+
public class ApplicationTests {
14+
15+
@Autowired
16+
private Task task;
17+
18+
@Test
19+
public void test() throws Exception {
20+
21+
task.doTaskOne();
22+
task.doTaskTwo();
23+
task.doTaskThree();
24+
25+
Thread.currentThread().join();
26+
}
27+
28+
}

0 commit comments

Comments
 (0)