-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
CustomThreadPool.java
403 lines (339 loc) · 9.78 KB
/
CustomThreadPool.java
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package com.crossoverjie.concurrent;
import com.crossoverjie.concurrent.communication.Notify;
import com.crossoverjie.concurrent.future.Callable;
import com.crossoverjie.concurrent.future.Future;
import com.crossoverjie.concurrent.future.FutureTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
/**
* Function:线程池
*
* @author crossoverJie
* Date: 2019-05-14 10:51
* @since JDK 1.8
*/
public class CustomThreadPool {
private final static Logger LOGGER = LoggerFactory.getLogger(CustomThreadPool.class);
private final ReentrantLock lock = new ReentrantLock();
/**
* 最小线程数,也叫核心线程数
*/
private volatile int miniSize;
/**
* 最大线程数
*/
private volatile int maxSize;
/**
* 线程需要被回收的时间
*/
private long keepAliveTime;
private TimeUnit unit;
/**
* 存放线程的阻塞队列
*/
private BlockingQueue<Runnable> workQueue;
/**
* 存放线程池
*/
private volatile Set<Worker> workers;
/**
* 是否关闭线程池标志
*/
private AtomicBoolean isShutDown = new AtomicBoolean(false);
/**
* 提交到线程池中的任务总数
*/
private AtomicInteger totalTask = new AtomicInteger();
/**
* 线程池任务全部执行完毕后的通知组件
*/
private Object shutDownNotify = new Object();
private Notify notify;
/**
* @param miniSize 最小线程数
* @param maxSize 最大线程数
* @param keepAliveTime 线程保活时间
* @param unit
* @param workQueue 阻塞队列
* @param notify 通知接口
*/
public CustomThreadPool(int miniSize, int maxSize, long keepAliveTime,
TimeUnit unit, BlockingQueue<Runnable> workQueue, Notify notify) {
this.miniSize = miniSize;
this.maxSize = maxSize;
this.keepAliveTime = keepAliveTime;
this.unit = unit;
this.workQueue = workQueue;
this.notify = notify;
workers = new ConcurrentHashSet<>();
}
/**
* 有返回值
*
* @param callable
* @param <T>
* @return
*/
public <T> Future<T> submit(Callable<T> callable) {
FutureTask<T> future = new FutureTask(callable);
execute(future);
return future;
}
/**
* 执行任务
*
* @param runnable 需要执行的任务
*/
public void execute(Runnable runnable) {
if (runnable == null) {
throw new NullPointerException("runnable nullPointerException");
}
if (isShutDown.get()) {
LOGGER.info("线程池已经关闭,不能再提交任务!");
return;
}
//提交的线程 计数
totalTask.incrementAndGet();
//小于最小线程数时新建线程
if (workers.size() < miniSize) {
addWorker(runnable);
return;
}
boolean offer = workQueue.offer(runnable);
//写入队列失败
if (!offer) {
//创建新的线程执行
if (workers.size() < maxSize) {
addWorker(runnable);
return;
} else {
LOGGER.error("超过最大线程数");
try {
//会阻塞
workQueue.put(runnable);
} catch (InterruptedException e) {
}
}
}
}
/**
* 添加任务,需要加锁
*
* @param runnable 任务
*/
private void addWorker(Runnable runnable) {
Worker worker = new Worker(runnable, true);
worker.startTask();
workers.add(worker);
}
/**
* 工作线程
*/
private final class Worker extends Thread {
private Runnable task;
private Thread thread;
/**
* true --> 创建新的线程执行
* false --> 从队列里获取线程执行
*/
private boolean isNewTask;
public Worker(Runnable task, boolean isNewTask) {
this.task = task;
this.isNewTask = isNewTask;
thread = this;
}
public void startTask() {
thread.start();
}
public void close() {
thread.interrupt();
}
@Override
public void run() {
Runnable task = null;
if (isNewTask) {
task = this.task;
}
boolean compile = true ;
try {
while ((task != null || (task = getTask()) != null)) {
try {
//执行任务
task.run();
} catch (Exception e) {
compile = false ;
throw e ;
} finally {
//任务执行完毕
task = null;
int number = totalTask.decrementAndGet();
//LOGGER.info("number={}",number);
if (number == 0) {
synchronized (shutDownNotify) {
shutDownNotify.notify();
}
}
}
}
} finally {
//释放线程
boolean remove = workers.remove(this);
//LOGGER.info("remove={},size={}", remove, workers.size());
if (!compile){
addWorker(null);
}
tryClose(true);
}
}
}
/**
* 从队列中获取任务
*
* @return
*/
private Runnable getTask() {
//关闭标识及任务是否全部完成
if (isShutDown.get() && totalTask.get() == 0) {
return null;
}
//while (true) {
//
// if (workers.size() > miniSize) {
// boolean value = number.compareAndSet(number.get(), number.get() - 1);
// if (value) {
// return null;
// } else {
// continue;
// }
// }
lock.lock();
try {
Runnable task = null;
if (workers.size() > miniSize) {
//大于核心线程数时需要用保活时间获取任务
task = workQueue.poll(keepAliveTime, unit);
} else {
task = workQueue.take();
}
if (task != null) {
return task;
}
} catch (InterruptedException e) {
return null;
} finally {
lock.unlock();
}
return null;
//}
}
/**
* 任务执行完毕后关闭线程池
*/
public void shutdown() {
isShutDown.set(true);
tryClose(true);
//中断所有线程
//synchronized (shutDownNotify){
// while (totalTask.get() > 0){
// try {
// shutDownNotify.wait();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
//}
}
/**
* 立即关闭线程池,会造成任务丢失
*/
public void shutDownNow() {
isShutDown.set(true);
tryClose(false);
}
/**
* 阻塞等到任务执行完毕
*/
public void mainNotify() {
synchronized (shutDownNotify) {
while (totalTask.get() > 0) {
try {
shutDownNotify.wait();
if (notify != null) {
notify.notifyListen();
}
} catch (InterruptedException e) {
return;
}
}
}
}
/**
* 关闭线程池
*
* @param isTry true 尝试关闭 --> 会等待所有任务执行完毕
* false 立即关闭线程池--> 任务有丢失的可能
*/
private void tryClose(boolean isTry) {
if (!isTry) {
closeAllTask();
} else {
if (isShutDown.get() && totalTask.get() == 0) {
closeAllTask();
}
}
}
/**
* 关闭所有任务
*/
private void closeAllTask() {
for (Worker worker : workers) {
//LOGGER.info("开始关闭");
worker.close();
}
}
/**
* 获取工作线程数量
*
* @return
*/
public int getWorkerCount() {
return workers.size();
}
/**
* 内部存放工作线程容器,并发安全。
*
* @param <T>
*/
private final class ConcurrentHashSet<T> extends AbstractSet<T> {
private ConcurrentHashMap<T, Object> map = new ConcurrentHashMap<>();
private final Object PRESENT = new Object();
private AtomicInteger count = new AtomicInteger();
@Override
public Iterator<T> iterator() {
return map.keySet().iterator();
}
@Override
public boolean add(T t) {
count.incrementAndGet();
return map.put(t, PRESENT) == null;
}
@Override
public boolean remove(Object o) {
count.decrementAndGet();
return map.remove(o) == PRESENT;
}
@Override
public int size() {
return count.get();
}
}
}