Skip to content

Commit 2d2eb53

Browse files
committed
CompletableFuture使用
1 parent f87a487 commit 2d2eb53

File tree

6 files changed

+181
-3
lines changed

6 files changed

+181
-3
lines changed

SpringBoot/AsyncDemo/src/main/java/com/example/async/config/ThreadPoolConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
5757
// 最大线程数
5858
threadPoolTaskExecutor.setMaxPoolSize(maxPoolSize);
5959
// 队列容量
60-
threadPoolTaskExecutor.setQueueCapacity(keepAliveSeconds);
60+
threadPoolTaskExecutor.setQueueCapacity(queueCapacity);
6161
// 活跃时间
62-
threadPoolTaskExecutor.setKeepAliveSeconds(queueCapacity);
62+
threadPoolTaskExecutor.setKeepAliveSeconds(keepAliveSeconds);
6363
// 主线程等待子线程执行时间
6464
threadPoolTaskExecutor.setAwaitTerminationSeconds(awaitTerminationSeconds);
6565
// 线程名字前缀

SpringBoot/AsyncDemo/src/main/resources/application.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ asyncThreadPool:
1010
# 最大线程数
1111
maxPoolSize: 30
1212
# 队列容量
13-
queueCapacity: 150
13+
queueCapacity: 15
1414
# 活跃时间
1515
keepAliveSeconds: 60
1616
# 主线程等待子线程执行时间

SpringBoot/ThreadPool/src/main/java/com/example/config/JdkThreadPoolConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.springframework.context.annotation.Configuration;
88
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
99

10+
import java.util.concurrent.LinkedBlockingQueue;
1011
import java.util.concurrent.SynchronousQueue;
1112
import java.util.concurrent.ThreadPoolExecutor;
1213
import java.util.concurrent.TimeUnit;
@@ -64,6 +65,7 @@ public ThreadPoolExecutor jdkThreadPoolExecutor() {
6465
corePoolSize,
6566
maxPoolSize,
6667
keepAliveSeconds, TimeUnit.SECONDS,
68+
// new LinkedBlockingQueue<>(queueCapacity),
6769
new ResizableCapacityLinkedBlockingQueue<>(queueCapacity),
6870
new CustomizableThreadFactory(threadNamePrefix),
6971
new ThreadPoolExecutor.CallerRunsPolicy());
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package com.example.controller;
2+
3+
import com.example.helper.BusinessHelper;
4+
import com.example.util.SmsUtil;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
import java.util.concurrent.CompletableFuture;
13+
import java.util.concurrent.Future;
14+
import java.util.concurrent.ThreadPoolExecutor;
15+
16+
/**
17+
* AsyncController
18+
*
19+
* @author wliduo[i@dolyw.com]
20+
* @date 2020/5/19 14:46
21+
*/
22+
@RestController
23+
@RequestMapping("/cf")
24+
public class CompletableFutureController {
25+
26+
/**
27+
* logger
28+
*/
29+
private final static Logger logger = LoggerFactory.getLogger(CompletableFutureController.class);
30+
31+
@Autowired
32+
private BusinessHelper businessHelper;
33+
34+
@Autowired
35+
private SmsUtil smsUtil;
36+
37+
@Autowired
38+
private ThreadPoolExecutor jdkThreadPoolExecutor;
39+
40+
@Autowired
41+
private ThreadPoolExecutor ioThreadPoolExecutor;
42+
43+
/**
44+
* 无返回值
45+
*
46+
* @param
47+
* @return void
48+
* @throws
49+
* @author wliduo[i@dolyw.com]
50+
* @date 2020/5/20 9:53
51+
*/
52+
@GetMapping("/run1")
53+
public String run1() throws Exception {
54+
logger.info("run1开始执行");
55+
CompletableFuture completableFuture = CompletableFuture.runAsync(() -> {
56+
try {
57+
businessHelper.handle1();
58+
} catch (Exception e) {
59+
60+
}
61+
});
62+
CompletableFuture completableFuture2 = CompletableFuture.runAsync(() -> {
63+
try {
64+
businessHelper.handle2();
65+
} catch (Exception e) {
66+
67+
}
68+
}, jdkThreadPoolExecutor);
69+
// Thread.sleep(5000);
70+
// 一个执行完就结束
71+
CompletableFuture.anyOf(completableFuture, completableFuture2).get();
72+
// 全部执行完才结束
73+
CompletableFuture.allOf(completableFuture, completableFuture2).get();
74+
logger.info("run1执行完成");
75+
return "run1 success";
76+
}
77+
78+
/**
79+
* 无返回值
80+
*
81+
* @param
82+
* @return void
83+
* @throws
84+
* @author wliduo[i@dolyw.com]
85+
* @date 2020/5/20 9:53
86+
*/
87+
@GetMapping("/run2")
88+
public String run2() throws Exception {
89+
logger.info("run2开始执行");
90+
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
91+
try {
92+
return businessHelper.handle1();
93+
} catch (Exception e) {
94+
return "";
95+
}
96+
}, ioThreadPoolExecutor);
97+
CompletableFuture<String> completableFuture2 = CompletableFuture.supplyAsync(() -> {
98+
try {
99+
// return businessHelper.handle2();
100+
return this.handle3();
101+
} catch (Exception e) {
102+
return "";
103+
}
104+
}, jdkThreadPoolExecutor);
105+
// Thread.sleep(5000);
106+
// 一个执行完就结束
107+
// CompletableFuture.anyOf(completableFuture, completableFuture2).get();
108+
// 全部执行完才结束
109+
CompletableFuture.allOf(completableFuture, completableFuture2).get();
110+
111+
logger.info(Boolean.toString(completableFuture.isDone()));
112+
logger.info(Boolean.toString(completableFuture2.isDone()));
113+
logger.info("run2执行完成");
114+
return "run2 success";
115+
}
116+
117+
public String handle3() throws Exception {
118+
Thread.sleep(3000L);
119+
logger.info("handle3");
120+
return "handle3";
121+
}
122+
123+
/**
124+
* 工具类异步
125+
*
126+
* @param
127+
* @return java.lang.String
128+
* @throws
129+
* @author wliduo[i@dolyw.com]
130+
* @date 2020/5/20 10:59
131+
*/
132+
@GetMapping("/sms")
133+
public String sms() throws Exception {
134+
logger.info("sms开始执行");
135+
smsUtil.send("15912347896", "135333");
136+
logger.info("sms执行完成");
137+
return "send sms success";
138+
}
139+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.example.helper;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.stereotype.Component;
6+
7+
/**
8+
* BusinessHelper
9+
*
10+
* @author wliduo[i@dolyw.com]
11+
* @date 2022/1/6 14:15
12+
*/
13+
@Component
14+
public class BusinessHelper {
15+
16+
private final static Logger logger = LoggerFactory.getLogger(BusinessHelper.class);
17+
18+
public String handle1() throws Exception {
19+
Thread.sleep(5000L);
20+
logger.info("handle1");
21+
return "handle1";
22+
}
23+
24+
public String handle2() throws Exception {
25+
Thread.sleep(3000L);
26+
logger.info("handle2");
27+
return "handle2";
28+
}
29+
30+
}

SpringBoot/ThreadPool/src/main/java/com/example/util/SmsUtil.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,11 @@ public void sendCode(String phone, String code) throws Exception {
3434
logger.info("发送成功: {}", phone);
3535
}
3636

37+
public void send(String phone, String code) throws Exception {
38+
logger.info("开始发送验证码...");
39+
// 模拟调用接口发验证码的耗时
40+
Thread.sleep(3000);
41+
logger.info("发送成功: {}", phone);
42+
}
43+
3744
}

0 commit comments

Comments
 (0)