-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPoolImpl.java
More file actions
138 lines (120 loc) · 4.15 KB
/
ThreadPoolImpl.java
File metadata and controls
138 lines (120 loc) · 4.15 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
package vr;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.IntStream;
public class ThreadPoolImpl {
private final List<Thread> threads = new LinkedList<>();
private final LinkedList<Future> taskList = new LinkedList<>();
public ThreadPoolImpl(int threadCount) {
IntStream.range(0, threadCount).forEach(i -> threads.add(new Thread(() -> {
Future future = null;
while (!Thread.interrupted()) {
try {
synchronized (taskList) {
while (taskList.isEmpty()) {
if (Thread.interrupted()) throw new InterruptedException();
taskList.wait();
}
future = taskList.poll();
}
future.calculateResult();
future = null;
} catch (InterruptedException e) {
break;
}
}
})));
threads.forEach(Thread::start);
}
public void shutdown() {
synchronized (taskList) {
taskList.forEach(future -> future.corrupt(new LightExecutionException()));
taskList.clear();
}
threads.forEach(Thread::interrupt);
}
public <T> LightFuture<T> submit(Supplier<T> supplier) {
Future<T> future = new Future<>(supplier);
synchronized (taskList) {
taskList.add(future);
taskList.notify();
}
return future;
}
public class Future<T> implements LightFuture<T> {
private final Supplier<T> supplier;
private volatile boolean isReady = false;
private volatile Exception exception = null;
private volatile T result = null;
private final List<Future> dependentTasks = new LinkedList<>();
public Future(Supplier<T> supplierForFuture) {
supplier = supplierForFuture;
}
public boolean isReady() {
return isReady;
}
public T get() throws Exception {
if (!isReady) {
synchronized (this) {
while (!isReady) {
this.wait();
}
}
}
if (exception != null) throw exception;
return result;
}
public <Y> LightFuture<Y> thenApply(Function<? super T, Y> function) {
Future<Y> future = new Future<>(() -> function.apply(getResult()));
synchronized (dependentTasks) {
if (isReady) {
if (exception != null) {
future.corrupt(exception);
} else {
synchronized (taskList) {
taskList.add(future);
taskList.notify();
}
}
} else {
dependentTasks.add(future);
}
}
return future;
}
private void calculateResult() {
try {
result = supplier.get();
synchronized (dependentTasks) {
synchronized (taskList) {
taskList.addAll(dependentTasks);
taskList.notifyAll();
dependentTasks.clear();
}
isReady = true;
}
} catch (Exception e) {
corrupt(new LightExecutionException());
}
synchronized (this) {
this.notifyAll();
}
}
private T getResult() {
return result;
}
private void corrupt(Exception exception) {
this.exception = exception;
synchronized (dependentTasks) {
dependentTasks.forEach(future -> future.corrupt(exception));
dependentTasks.clear();
isReady = true;
}
synchronized (this) {
this.notifyAll();
}
}
}
}