-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicThreadPoolTestApplicationTests.java
More file actions
119 lines (98 loc) · 4 KB
/
Copy pathDynamicThreadPoolTestApplicationTests.java
File metadata and controls
119 lines (98 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.yw.dynamicthreadpooltest;
import cn.hutool.core.thread.ThreadUtil;
import com.yw.sdk.domain.model.entity.ThreadPoolConfigEntity;
import com.yw.sdk.domain.model.valobj.RegistryEnumVO;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.concurrent.*;
@SpringBootTest
@Slf4j
class DynamicThreadPoolTestApplicationTests {
@Resource
private RedissonClient redissonClient;
@Test
void contextLoads() {
RTopic rTopic = redissonClient.getTopic(RegistryEnumVO.DYNAMIC_THREAD_POOL_REDIS_TOPIC.getKey() + "_" + "dynamic-thread-pool-test");
ThreadPoolConfigEntity threadPoolConfigEntity = new ThreadPoolConfigEntity("test", "threadPollExecutorOne");
threadPoolConfigEntity.setActiveCount(1);
threadPoolConfigEntity.setCorePoolSize(1);
threadPoolConfigEntity.setMaximumPoolSize(1);
rTopic.publish(threadPoolConfigEntity);
}
public static void main(String[] args) {
try {
log.info("开始执行");
test();
} catch (Exception e) {
log.info("异常"+e);
e.printStackTrace();
}
}
public static void test() throws InterruptedException {
// ExecutorService executor = ThreadUtil.newExecutor();
ExecutorService executor = Executors.newFixedThreadPool(5);
// kc_customer
CompletableFuture<Void> task1 = CompletableFuture.runAsync(() -> {
log.info("开始更新kc_customer");
log.info("更新kc_customer完毕");
}, executor);
// kc_customer_detail_log
CompletableFuture<Void> task2 = CompletableFuture.runAsync(() -> {
log.info("开始更新kc_customer_detail_log");
log.info("更新kc_customer_detail_log完毕");
}, executor);
// kc_free_course_ask
CompletableFuture<Void> task3 = CompletableFuture.runAsync(() -> {
log.info("开始更新kc_free_course_ask");
log.info("更新kc_free_course_ask完毕");
}, executor);
// kc_sms_send_records
CompletableFuture<Void> task4 = CompletableFuture.runAsync(() -> {
log.info("开始更新kc_sms_send_records");
log.info("更新kc_sms_send_records完毕");
}, executor);
// kc_statistics_log
CompletableFuture<Void> task5 = CompletableFuture.runAsync(() -> {
log.info("开始更新kc_statistics_log");
log.info("更新kc_statistics_log完毕");
}, executor);
CompletableFuture.allOf(task1,task2,task3,task4,task5).join();
log.info("更新部门code完毕");
}
public static void test2() throws InterruptedException, ExecutionException {
CompletionService<Boolean> completionService = ThreadUtil.newCompletionService();
Future<Boolean> future1 = completionService.submit((() -> {
// Thread.sleep(100000);
int i = 1 / 0;
log.info("更新kc_customer完毕");
}), true);
Future<Boolean> future2 = completionService.submit((() -> {
// Thread.sleep(100000);
int i = 1 / 0;
log.info("更新kc_customer完毕");
}), true);
future1.get();
future2.get();
//收集这些线程执行结果
completionService.take();
log.info("结束");
}
public static void test3() {
ExecutorService executor = Executors.newFixedThreadPool(5);
// kc_customer
CompletableFuture<Void> task1 = CompletableFuture.runAsync(() -> {
// try {
// Thread.sleep(100000);
// } catch (InterruptedException e) {
// throw new RuntimeException(e);
// }
int i = 1 / 0;
}, executor);
CompletableFuture.allOf(task1).join();
log.info("更新部门code完毕");
}
}