forked from guangxush/wheel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicThreadPool.java
More file actions
190 lines (165 loc) · 5.63 KB
/
Copy pathBasicThreadPool.java
File metadata and controls
190 lines (165 loc) · 5.63 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
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author: guangxush
* @create: 2019/08/24
*/
public class BasicThreadPool extends Thread implements ThreadPool {
private final int initSize;
private final int maxSize;
private final int coreSize;
private int activeCount;
private final ThreadFactory threadFactory;
private final RunnableQueue runnableQueue;
private volatile boolean isShutDown = false;
private final Queue<ThreadTask> threadQueue = new ArrayDeque<>();
private final static DenyPolicy DEFAULT_DENY_POLICY = new DenyPolicy.DiscardDenyPolicy();
private final static ThreadFactory DEFAULT_THREAD_FACTORY = new DefaultThreadFactory();
private final long keepAliveTime;
private final TimeUnit timeUnit;
public BasicThreadPool(int initSize, int maxSize, int coreSize, int queueSize) {
this(initSize, maxSize, coreSize, DEFAULT_THREAD_FACTORY, queueSize, DEFAULT_DENY_POLICY, 10, TimeUnit.SECONDS);
}
public BasicThreadPool(int initSize, int maxSize, int coreSize, ThreadFactory threadFactory, int queueSize, DenyPolicy denyPolicy, long keepAliveTime, TimeUnit timeUnit) {
this.initSize = initSize;
this.maxSize = maxSize;
this.coreSize = coreSize;
this.activeCount = activeCount;
this.threadFactory = threadFactory;
this.runnableQueue = new LinkedRunnableQueue(
queueSize, denyPolicy, this
);
this.keepAliveTime = keepAliveTime;
this.timeUnit = timeUnit;
this.init();
}
private void init() {
start();
for (int i = 0; i < initSize; i++) {
new Thread();
}
}
@Override
public void execute(Runnable runnable) {
if (this.isShutDown) {
throw new IllegalStateException("This thread pool is destroy");
}
this.runnableQueue.offer(runnable);
}
@Override
public void shutdown() {
synchronized (this){
if(isShutDown){
return;
}
isShutDown = true;
threadQueue.forEach(threadTask -> {
threadTask.internalTask.stop();
threadTask.thread.interrupt();
});
this.interrupt();
}
}
@Override
public int getInitSize() {
if(isShutDown){
throw new IllegalStateException("This thread pool is destroy");
}
return this.initSize;
}
@Override
public int getMaxSize() {
if(isShutDown){
throw new IllegalStateException("This thread pool is destroy");
}
return this.maxSize;
}
@Override
public int getCoreSize() {
if(isShutDown){
throw new IllegalStateException("This thread pool is destroy");
}
return this.coreSize;
}
@Override
public int getQueueSize() {
if(isShutDown){
throw new IllegalStateException("This thread pool is destroy");
}
return runnableQueue.size();
}
@Override
public int getActiveCount() {
synchronized (this){
return this.activeCount;
}
}
@Override
public boolean isShutdown() {
return this.isShutDown;
}
@Override
public void run() {
while (!isShutDown && !isInterrupted()) {
try {
timeUnit.sleep(keepAliveTime);
} catch (InterruptedException e) {
isShutDown = true;
break;
}
synchronized (this) {
if (isShutDown) {
break;
}
if (runnableQueue.size() > 0 && activeCount < coreSize) {
for (int i = initSize; i < coreSize; i++) {
newThread();
}
continue;
}
if (runnableQueue.size() > 0 && activeCount < maxSize) {
for (int i = coreSize; i < maxSize; i++) {
newThread();
}
}
if (runnableQueue.size() == 0 && activeCount > coreSize) {
for (int i = coreSize; i < activeCount; i++) {
removeThread();
}
}
}
}
}
public static class ThreadTask {
Thread thread;
InternalTask internalTask;
public ThreadTask(Thread thread, InternalTask internalTask) {
this.thread = thread;
this.internalTask = internalTask;
}
}
public static class DefaultThreadFactory implements ThreadFactory {
private static final AtomicInteger COUNT = new AtomicInteger(0);
private static final AtomicInteger GROUP_COUNTER = new AtomicInteger(1);
private static final ThreadGroup group = new ThreadGroup("MyThreadPool-" + GROUP_COUNTER.getAndDecrement());
@Override
public Thread createThread(Runnable runnable) {
return new Thread(group, runnable, "thread-pool-" + COUNT.getAndDecrement());
}
}
private void newThread() {
InternalTask internalTask = new InternalTask(runnableQueue);
Thread thread = this.threadFactory.createThread(internalTask);
ThreadTask threadTask = new ThreadTask(thread, internalTask);
threadQueue.offer(threadTask);
this.activeCount++;
thread.start();
}
private void removeThread() {
ThreadTask threadTask = threadQueue.remove();
threadTask.internalTask.stop();
this.activeCount--;
}
}