-
-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathThreadPool.java
More file actions
176 lines (161 loc) · 5.81 KB
/
ThreadPool.java
File metadata and controls
176 lines (161 loc) · 5.81 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
package org.dreeam.leaf.async;
import net.minecraft.util.Util;
import org.apache.logging.log4j.LogManager;
import org.dreeam.leaf.util.queue.MpmcQueue;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
import org.apache.logging.log4j.Logger;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;
@NullMarked
public final class ThreadPool implements Executor {
private static final Logger LOGGER = LogManager.getLogger("Leaf");
private static final long PARK_NANOS = 200_000L; // 0.2ms
private volatile boolean shutdown = false;
private final Thread[] threads;
private final MpmcQueue<Runnable> channel;
private final MpmcQueue<Thread> parkChannel;
public ThreadPool(int numThreads, final int queue, final String prefix, final int priority) {
if (numThreads <= 0) {
throw new IllegalArgumentException();
}
numThreads = numThreads + 1;
this.threads = new Thread[numThreads];
this.channel = new MpmcQueue<>(queue);
this.parkChannel = new MpmcQueue<>(numThreads);
this.threads[0] = Thread.ofPlatform()
.uncaughtExceptionHandler(Util::onThreadException)
.daemon(false)
.priority(priority + 1)
.name(prefix + " Dispatcher")
.start(new Dispatcher(this));
for (int i = 1; i < numThreads; i++) {
threads[i] = Thread.ofPlatform()
.uncaughtExceptionHandler(Util::onThreadException)
.daemon(false)
.priority(priority)
.name(prefix + " Worker - " + i)
.start(new Worker(this));
}
}
@Override
public void execute(Runnable task) {
if (shutdown || !channel.send(task)) {
task.run();
}
}
public boolean isShutdown() {
return shutdown;
}
public <V> FutureTask<V> submit(Runnable task, @Nullable V result) {
final FutureTask<V> t = new FutureTask<>(Executors.callable(task, result));
execute(t);
return t;
}
public <V> FutureTask<V> submit(Callable<V> task) {
final FutureTask<V> t = new FutureTask<>(task);
execute(t);
return t;
}
public void shutdown() {
shutdown = true;
for (final Thread thread : threads) {
LockSupport.unpark(thread);
}
}
public boolean awaitTermination(final long timeout, final TimeUnit unit) throws InterruptedException {
final long nanos = unit.toNanos(timeout);
final long startTime = System.nanoTime();
boolean flag = true;
for (final Thread worker : threads) {
if (nanos <= 0L) {
worker.join();
continue;
}
final long remaining = startTime + nanos - System.nanoTime();
if (remaining <= 0L) {
flag = false;
break;
} else {
worker.join(remaining / 1_000_000L, (int) (remaining % 1_000_000L));
if (worker.isAlive()) {
flag = false;
break;
}
}
}
Runnable task;
while ((task = channel.recv()) != null) {
task.run();
}
return flag;
}
public int workerCount() {
return threads.length - 1;
}
private record Worker(ThreadPool executor) implements Runnable {
@Override
public void run() {
final MpmcQueue<Runnable> channel = executor.channel;
final MpmcQueue<Thread> park = executor.parkChannel;
while (true) {
final Runnable task = channel.recv();
if (task != null) {
try {
task.run();
} catch (final Throwable e) {
LOGGER.error("Task {} generated an exception: {}", task, Thread.currentThread().getName(), e);
}
} else if (executor.shutdown) {
break;
} else if (park.send(Thread.currentThread())) {
LockSupport.park();
if (Thread.interrupted()) {
Thread.currentThread().interrupt();
break;
}
} else {
Thread.yield();
}
}
}
}
private record Dispatcher(ThreadPool executor) implements Runnable {
@Override
public void run() {
final int threads = executor.threads.length - 1;
final MpmcQueue<Runnable> channel = executor.channel;
final MpmcQueue<Thread> park = executor.parkChannel;
int backoff = 0;
while (true) {
final int len = channel.length();
if (len != 0 && threads - park.length() < len) {
backoff = 0;
final Thread thread = park.recv();
if (thread != null) {
LockSupport.unpark(thread);
}
} else if (executor.shutdown) {
break;
} else if (backoff < 8) {
backoff++;
Thread.yield();
} else {
LockSupport.parkNanos(PARK_NANOS);
if (Thread.interrupted()) {
Thread.currentThread().interrupt();
break;
}
}
}
Thread left;
while ((left = park.recv()) != null) {
LockSupport.unpark(left);
}
}
}
}