-
Notifications
You must be signed in to change notification settings - Fork 0
/
TaskTrackingThreadPoolExecutor.java
224 lines (171 loc) · 6.19 KB
/
TaskTrackingThreadPoolExecutor.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
// Copyright 2023 Piotr Morgwai Kotarbinski, Licensed under the Apache License, Version 2.0
package pl.morgwai.base.utils.concurrent;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.BiConsumer;
import pl.morgwai.base.utils.concurrent.TaskTrackingExecutor.TaskTrackingExecutorDecorator;
import static java.util.concurrent.TimeUnit.DAYS;
/** A {@link TaskTrackingExecutor} based on a {@link ThreadPoolExecutor}. */
public class TaskTrackingThreadPoolExecutor extends ThreadPoolExecutor
implements TaskTrackingExecutor, TaskTrackingExecutorDecorator.HookableExecutor {
final TaskTrackingExecutorDecorator taskTrackingDecorator;
/**
* Calls {@link ThreadPoolExecutor#ThreadPoolExecutor(int, int, long, TimeUnit, BlockingQueue,
* ThreadFactory, RejectedExecutionHandler) super} and sets up
* {@link TaskTrackingExecutor.TaskTrackingExecutorDecorator}.
*/
public TaskTrackingThreadPoolExecutor(
int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler
) {
super(
corePoolSize,
maximumPoolSize,
keepAliveTime,
unit,
workQueue,
threadFactory,
handler
);
taskTrackingDecorator = createAndSetupTaskTrackingDecorator();
}
TaskTrackingExecutorDecorator createAndSetupTaskTrackingDecorator() {
final var decorator = new TaskTrackingExecutorDecorator(this, getCorePoolSize());
setThreadFactory(decorator.decorateThreadFactory(getThreadFactory()));
return decorator;
}
@Override
public List<Runnable> getRunningTasks() {
return taskTrackingDecorator.getRunningTasks();
}
final Deque<BiConsumer<Thread, Runnable>> beforeExecuteHooks = new LinkedList<>();
/** Adds {@code hook} to be executed in {@link #beforeExecute(Thread, Runnable)}. */
public void addBeforeExecuteHook(BiConsumer<Thread, Runnable> hook) {
beforeExecuteHooks.addFirst(hook);
}
/**
* Executes all {@link #addBeforeExecuteHook(BiConsumer) added hooks} in the reverse order they
* were added.
*/
@Override
protected final void beforeExecute(Thread worker, Runnable task) {
beforeExecuteHooks.forEach((hook) -> hook.accept(worker, task));
}
final List<BiConsumer<Runnable, Throwable>> afterExecuteHooks = new LinkedList<>();
/** Adds hook to be executed in {@link #afterExecute(Runnable, Throwable)}. */
public void addAfterExecuteHook(BiConsumer<Runnable, Throwable> hook) {
afterExecuteHooks.add(hook);
}
/**
* Executes all {@link #addAfterExecuteHook(BiConsumer)} added hooks} in the order they were
* added.
*/
@Override
protected final void afterExecute(Runnable task, Throwable error) {
afterExecuteHooks.forEach((hook) -> hook.accept(task, error));
}
// only constructor variations below
/**
* Calls {@link #TaskTrackingThreadPoolExecutor(int, int, long, TimeUnit, BlockingQueue)
* this(poolSize, poolSize, 0L, DAYS, new LinkedBlockingQueue<>())}.
*/
public TaskTrackingThreadPoolExecutor(int poolSize) {
this(poolSize, poolSize, 0L, DAYS, new LinkedBlockingQueue<>());
}
/**
* Calls {@link #TaskTrackingThreadPoolExecutor(int, int, long, TimeUnit, BlockingQueue)
* this(poolSize, poolSize, 0L, DAYS, new LinkedBlockingQueue<>(queueSize))}.
*/
public TaskTrackingThreadPoolExecutor(int poolSize, int queueSize) {
this(poolSize, poolSize, 0L, DAYS, new LinkedBlockingQueue<>(queueSize));
}
/**
* Calls {@link #TaskTrackingThreadPoolExecutor(int, int, long, TimeUnit, BlockingQueue)
* this(poolSize, poolSize, 0L, DAYS, new LinkedBlockingQueue<>(queueSize), threadFactory)}.
*/
public TaskTrackingThreadPoolExecutor(int poolSize, int queueSize, ThreadFactory threadFactory)
{
this(poolSize, poolSize, 0L, DAYS, new LinkedBlockingQueue<>(queueSize), threadFactory);
}
/**
* Calls {@link #TaskTrackingThreadPoolExecutor(int, int, long, TimeUnit, BlockingQueue)
* this(poolSize, poolSize, 0L, DAYS, new LinkedBlockingQueue<>(queueSize), handler)}.
*/
public TaskTrackingThreadPoolExecutor(
int poolSize,
int queueSize,
RejectedExecutionHandler handler
) {
this(poolSize, poolSize, 0L, DAYS, new LinkedBlockingQueue<>(queueSize), handler);
}
/**
* Calls {@link #TaskTrackingThreadPoolExecutor(int, int, long, TimeUnit, BlockingQueue)
* this(poolSize, poolSize, 0L, DAYS, new LinkedBlockingQueue<>(queueSize), threadFactory,
* handler)}.
*/
public TaskTrackingThreadPoolExecutor(
int poolSize,
int queueSize,
ThreadFactory threadFactory,
RejectedExecutionHandler handler
) {
this(
poolSize,
poolSize,
0L, DAYS,
new LinkedBlockingQueue<>(queueSize),
threadFactory,
handler
);
}
/**
* Calls {@link ThreadPoolExecutor#ThreadPoolExecutor(int, int, long, TimeUnit, BlockingQueue)
* super} and sets up {@link TaskTrackingExecutor.TaskTrackingExecutorDecorator}.
*/
public TaskTrackingThreadPoolExecutor(
int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue
) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
taskTrackingDecorator = createAndSetupTaskTrackingDecorator();
}
/**
* Calls {@link ThreadPoolExecutor#ThreadPoolExecutor(int, int, long, TimeUnit, BlockingQueue,
* ThreadFactory) super} and sets up {@link TaskTrackingExecutor.TaskTrackingExecutorDecorator}.
*/
public TaskTrackingThreadPoolExecutor(
int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory
) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
taskTrackingDecorator = createAndSetupTaskTrackingDecorator();
}
/**
* Calls {@link ThreadPoolExecutor#ThreadPoolExecutor(int, int, long, TimeUnit, BlockingQueue,
* RejectedExecutionHandler) super} and sets up
* {@link TaskTrackingExecutor.TaskTrackingExecutorDecorator}.
*/
public TaskTrackingThreadPoolExecutor(
int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
RejectedExecutionHandler handler
) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
taskTrackingDecorator = createAndSetupTaskTrackingDecorator();
}
}