CustomThreadPool.java
5.8 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package com.skua.tool.pool;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.concurrent.*;
/**
* @Author sonin
* @Date 2021/6/21 16:57
*/
public class CustomThreadPool {
private static final Log log = LogFactory.getLog(CustomThreadPool.class);
private String threadPoolName;
private Boolean daemon;
private Integer priority;
private UncaughtExceptionHandler uncaughtExceptionHandler;
private Integer corePoolSize;
private Integer maximumPoolSize;
private Long keepAliveTime;
private TimeUnit timeUnit;
private Integer queueCapacity;
private RejectedExecutionHandler rejectedExecutionHandler;
private CustomThreadPool() {
threadPoolName = "default-" + System.currentTimeMillis();
daemon = false;
priority = 5;
uncaughtExceptionHandler = (thread, exception) -> {
log.error(thread.getName() + " => " + exception.getMessage());
};
corePoolSize = Runtime.getRuntime().availableProcessors();
maximumPoolSize = 2 * Runtime.getRuntime().availableProcessors();
keepAliveTime = 0L;
timeUnit = TimeUnit.MICROSECONDS;
queueCapacity = 10 * Runtime.getRuntime().availableProcessors();
rejectedExecutionHandler = new ThreadPoolExecutor.AbortPolicy();
}
private String getThreadPoolName() {
return threadPoolName;
}
private void setThreadPoolName(String threadPoolName) {
this.threadPoolName = threadPoolName;
}
private Boolean getDaemon() {
return daemon;
}
private void setDaemon(Boolean daemon) {
this.daemon = daemon;
}
private Integer getPriority() {
return priority;
}
private void setPriority(Integer priority) {
this.priority = priority;
}
private UncaughtExceptionHandler getUncaughtExceptionHandler() {
return uncaughtExceptionHandler;
}
private void setUncaughtExceptionHandler(UncaughtExceptionHandler uncaughtExceptionHandler) {
this.uncaughtExceptionHandler = uncaughtExceptionHandler;
}
private Integer getCorePoolSize() {
return corePoolSize;
}
private void setCorePoolSize(Integer corePoolSize) {
this.corePoolSize = corePoolSize;
}
private Integer getMaximumPoolSize() {
return maximumPoolSize;
}
private void setMaximumPoolSize(Integer maximumPoolSize) {
this.maximumPoolSize = maximumPoolSize;
}
private Long getKeepAliveTime() {
return keepAliveTime;
}
private void setKeepAliveTime(Long keepAliveTime) {
this.keepAliveTime = keepAliveTime;
}
private TimeUnit getTimeUnit() {
return timeUnit;
}
private void setTimeUnit(TimeUnit timeUnit) {
this.timeUnit = timeUnit;
}
private Integer getQueueCapacity() {
return queueCapacity;
}
private void setQueueCapacity(Integer queueCapacity) {
this.queueCapacity = queueCapacity;
}
private RejectedExecutionHandler getRejectedExecutionHandler() {
return rejectedExecutionHandler;
}
private void setRejectedExecutionHandler(RejectedExecutionHandler rejectedExecutionHandler) {
this.rejectedExecutionHandler = rejectedExecutionHandler;
}
public static class Builder {
private final CustomThreadPool customThreadPool;
public Builder() {
customThreadPool = new CustomThreadPool();
}
public ThreadPoolExecutor build() {
String threadPoolName = customThreadPool.getThreadPoolName();
Boolean daemon = customThreadPool.getDaemon();
Integer priority = customThreadPool.getPriority();
UncaughtExceptionHandler uncaughtExceptionHandler = customThreadPool.getUncaughtExceptionHandler();
Integer corePoolSize = customThreadPool.getCorePoolSize();
Integer maximumPoolSize = customThreadPool.getMaximumPoolSize();
Long keepAliveTime = customThreadPool.getKeepAliveTime();
TimeUnit timeUnit = customThreadPool.getTimeUnit();
Integer queueCapacity = customThreadPool.getQueueCapacity();
RejectedExecutionHandler rejectedExecutionHandler = customThreadPool.getRejectedExecutionHandler();
ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("pool-" + threadPoolName + "-%d").setDaemon(daemon).setPriority(priority).setUncaughtExceptionHandler(uncaughtExceptionHandler).build();
return new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, timeUnit, new LinkedBlockingQueue<>(queueCapacity), threadFactory, rejectedExecutionHandler);
}
public Builder threadPoolName(String threadPoolName) {
customThreadPool.setThreadPoolName(threadPoolName);
return this;
}
public Builder daemon(Boolean daemon) {
customThreadPool.setDaemon(daemon);
return this;
}
public Builder priority(Integer priority) {
customThreadPool.setPriority(priority);
return this;
}
public Builder uncaughtExceptionHandler(UncaughtExceptionHandler uncaughtExceptionHandler) {
customThreadPool.setUncaughtExceptionHandler(uncaughtExceptionHandler);
return this;
}
public Builder corePoolSize(Integer corePoolSize) {
customThreadPool.setCorePoolSize(corePoolSize);
if (customThreadPool.getMaximumPoolSize() < corePoolSize) {
customThreadPool.setMaximumPoolSize(corePoolSize);
}
return this;
}
public Builder maximumPoolSize(Integer maximumPoolSize) {
customThreadPool.setMaximumPoolSize(maximumPoolSize);
if (customThreadPool.getCorePoolSize() > maximumPoolSize) {
customThreadPool.setCorePoolSize(maximumPoolSize);
}
return this;
}
public Builder keepAliveTime(Long keepAliveTime) {
customThreadPool.setKeepAliveTime(keepAliveTime);
return this;
}
public Builder timeUnit(TimeUnit timeUnit) {
customThreadPool.setTimeUnit(timeUnit);
return this;
}
public Builder queueCapacity(Integer queueCapacity) {
customThreadPool.setQueueCapacity(queueCapacity);
return this;
}
public Builder rejectedExecutionHandler(RejectedExecutionHandler rejectedExecutionHandler) {
customThreadPool.setRejectedExecutionHandler(rejectedExecutionHandler);
return this;
}
}
}