高并发编程-自定义简易的线程池(2),体会原理
【摘要】
文章目录
概述示例
概述
高并发编程-自定义简易的线程池(1),体会原理 中只实现了任务队列,我们这里把其余的几个也补充进来
拒绝策略关闭线程池最小 最大 活动线程数…
示例...
概述
高并发编程-自定义简易的线程池(1),体会原理 中只实现了任务队列,我们这里把其余的几个也补充进来
- 拒绝策略
- 关闭线程池
- 最小 最大 活动线程数
- …
示例
比较简单,直接上代码,见注释
package com.artisan.test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class SimpleThreadPool extends Thread {
// 线程数
private int size;
// 队列大小
private final int queueSize;
// 默认队列大小
private final static int DEFAULT_TASK_QUEUE_SIZE = 2000;
// 工作线程相关属性
private static volatile int seq = 0;
private final static String THREAD_PREFIX = "SIMPLE_THREAD_POOL-";
private final static ThreadGroup GROUP = new ThreadGroup("Pool_Group");
// 任务队列
private final static LinkedList<Runnable> TASK_QUEUE = new LinkedList<>();
// 线程队列
private final static List<WorkerTask> THREAD_QUEUE = new ArrayList<>();
// 拒绝策略
private final DiscardPolicy discardPolicy;
// 默认拒绝策略
public final static DiscardPolicy DEFAULT_DISCARD_POLICY = () -> {
throw new DiscardException("Discard This Task.");
};
//是否销毁
private volatile boolean destroy = false;
// 最小线程数
private int min;
// 最大线程数
private int max;
// 活动线程数
private int active;
/**
* 默认无参构造函数
*/
public SimpleThreadPool() {
// 调用有参构造函数
this(4, 8, 12, DEFAULT_TASK_QUEUE_SIZE, DEFAULT_DISCARD_POLICY);
}
/**
* 构造函数
* @param min
* @param active
* @param max
* @param queueSize
* @param discardPolicy
*/
public SimpleThreadPool(int min, int active, int max, int queueSize, DiscardPolicy discardPolicy) {
this.min = min;
this.active = active;
this.max = max;
this.queueSize = queueSize;
this.discardPolicy = discardPolicy;
init();
}
/**
* 初始化方法
*/
private void init() {
for (int i = 0; i < this.min; i++) {
//
createWorkTask();
}
this.size = min;
this.start();
}
/**
* 提交任务到 TASK_QUEUE
* @param runnable
*/
public void submit(Runnable runnable) {
if (destroy)
throw new IllegalStateException("The thread pool already destroy and not allow submit task.");
synchronized (TASK_QUEUE) {
if (TASK_QUEUE.size() > queueSize)
discardPolicy.discard();
TASK_QUEUE.addLast(runnable);
TASK_QUEUE.notifyAll();
}
}
/**
* 管理线程池本身的参数
*/
@Override
public void run() {
while (!destroy) {
System.out.printf("Pool#Min:%d,Active:%d,Max:%d,Current:%d,QueueSize:%d\n",
this.min, this.active, this.max, this.size, TASK_QUEUE.size());
try {
Thread.sleep(5_000L);
if (TASK_QUEUE.size() > active && size < active) {
for (int i = size; i < active; i++) {
createWorkTask();
}
System.out.println("The pool incremented to active.");
size = active;
} else if (TASK_QUEUE.size() > max && size < max) {
for (int i = size; i < max; i++) {
createWorkTask();
}
System.out.println("The pool incremented to max.");
size = max;
}
synchronized (THREAD_QUEUE) {
if (TASK_QUEUE.isEmpty() && size > active) {
System.out.println("=========Reduce========");
int releaseSize = size - active;
for (Iterator<WorkerTask> it = THREAD_QUEUE.iterator(); it.hasNext(); ) {
if (releaseSize <= 0)
break;
WorkerTask task = it.next();
task.close();
task.interrupt();
it.remove();
releaseSize--;
}
size = active;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* 创建工作线程
*/
private void createWorkTask() {
WorkerTask task = new WorkerTask(GROUP, THREAD_PREFIX + (seq++));
task.start();
THREAD_QUEUE.add(task);
}
/**
* 关闭线程池
* @throws InterruptedException
*/
public void shutdown() throws InterruptedException {
while (!TASK_QUEUE.isEmpty()) {
Thread.sleep(50);
}
synchronized (THREAD_QUEUE) {
int initVal = THREAD_QUEUE.size();
while (initVal > 0) {
for (WorkerTask task : THREAD_QUEUE) {
if (task.getTaskState() == TaskState.BLOCKED) {
task.interrupt();
task.close();
initVal--;
} else {
Thread.sleep(10);
}
}
}
}
System.out.println(GROUP.activeCount());
this.destroy = true;
System.out.println("The thread pool disposed.");
}
public int getQueueSize() {
return queueSize;
}
public int getSize() {
return size;
}
public boolean isDestroy() {
return this.destroy;
}
public int getMin() {
return min;
}
public int getMax() {
return max;
}
public int getActive() {
return active;
}
/**
* 工作线程的状态
*/
private enum TaskState {
FREE, RUNNING, BLOCKED, DEAD
}
/**
* 触发决绝策略抛出的异常
*/
public static class DiscardException extends RuntimeException {
public DiscardException(String message) {
super(message);
}
}
/**
* 拒绝策略接口
*/
public interface DiscardPolicy {
void discard() throws DiscardException;
}
/**
* 工作线程
*/
private static class WorkerTask extends Thread {
private volatile TaskState taskState = TaskState.FREE;
public WorkerTask(ThreadGroup group, String name) {
super(group, name);
}
public TaskState getTaskState() {
return this.taskState;
}
public void run() {
OUTER:
while (this.taskState != TaskState.DEAD) {
Runnable runnable;
synchronized (TASK_QUEUE) {
while (TASK_QUEUE.isEmpty()) {
try {
taskState = TaskState.BLOCKED;
TASK_QUEUE.wait();
} catch (InterruptedException e) {
System.out.println("Closed.");
break OUTER;
}
}
runnable = TASK_QUEUE.removeFirst();
}
if (runnable != null) {
taskState = TaskState.RUNNING;
runnable.run();
taskState = TaskState.FREE;
}
}
}
/**
* 关闭
*/
public void close() {
this.taskState = TaskState.DEAD;
}
}
/**
* 测试
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
SimpleThreadPool threadPool = new SimpleThreadPool();
for (int i = 0; i < 40; i++) {
threadPool.submit(() -> {
System.out.println("The runnable be serviced by " + Thread.currentThread() + " start.");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("The runnable be serviced by " + Thread.currentThread() + " finished.");
});
}
Thread.sleep(10000);
threadPool.shutdown();
/* Thread.sleep(10000);
threadPool.shutdown();
threadPool.submit(() -> System.out.println("======="));*/
}
}
- 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
运行日志:
"E:\Program Files\Java\jdk1.8.0_161\bin\java" "-javaagent:E:\Program Files\JetBrains\IntelliJ IDEA 2017.2.4\lib\idea_rt.jar=53638:E:\Program Files\JetBrains\IntelliJ IDEA 2017.2.4\bin" -Dfile.encoding=UTF-8 -classpath "E:\Program Files\Java\jdk1.8.0_161\jre\lib\charsets.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\deploy.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\access-bridge-64.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\cldrdata.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\dnsns.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\jaccess.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\jfxrt.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\localedata.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\nashorn.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunec.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunjce_provider.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunmscapi.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunpkcs11.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\zipfs.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\javaws.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\jce.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\jfr.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\jfxswt.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\jsse.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\management-agent.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\plugin.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\resources.jar;E:\Program Files\Java\jdk1.8.0_161\jre\lib\rt.jar;D:\IdeaProjects\mvc\target\classes" com.artisan.test.SimpleThreadPool
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] start.
Pool#Min:4,Active:8,Max:12,Current:4,QueueSize:38
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] start.
The pool incremented to active.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] start.
Pool#Min:4,Active:8,Max:12,Current:8,QueueSize:31
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] start.
The pool incremented to max.
Pool#Min:4,Active:8,Max:12,Current:12,QueueSize:15
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-10,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-11,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-10,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-11,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-10,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-11,5,Pool_Group] start.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-4,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-7,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-6,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-5,5,Pool_Group] finished.
Closed.
Closed.
Closed.
Closed.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-0,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-2,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-1,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-3,5,Pool_Group] finished.
Closed.
Closed.
Closed.
Closed.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-8,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-10,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-11,5,Pool_Group] finished.
The runnable be serviced by Thread[SIMPLE_THREAD_POOL-9,5,Pool_Group] finished.
Closed.
4
=========Reduce========
The thread pool disposed.
Closed.
Closed.
Closed.
Process finished with exit code 0
- 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
文章来源: artisan.blog.csdn.net,作者:小小工匠,版权归原作者所有,如需转载,请联系作者。
原文链接:artisan.blog.csdn.net/article/details/102752098
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)