-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScheduledTaskTrackingThreadPoolExecutor.java
189 lines (134 loc) · 5.42 KB
/
ScheduledTaskTrackingThreadPoolExecutor.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
// 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;
/**
* A {@link ScheduledThreadPoolExecutor} that is also a {@link TaskTrackingExecutor}.
* Decorates scheduled items with {@link ScheduledExecution}, so that the original tasks can be
* obtained from the result of {@link #getRunningTasks()}.
* <p>
* <b>NOTE:</b> due to the design of
* {@link ScheduledThreadPoolExecutor#decorateTask(Runnable, RunnableScheduledFuture) task
* decorating in ScheduledThreadPoolExecutor} this class is <i>extremely</i> slow in case of
* a large number of very tiny tasks. See
* <a href="https://github.com/AdoptOpenJDK/openjdk-jdk11/blob/master/src/java.base/share/classes/
java/util/concurrent/ScheduledThreadPoolExecutor.java#L903-L915">
* the comment with an explanation in the source</a>.</p>
*/
public class ScheduledTaskTrackingThreadPoolExecutor extends ScheduledThreadPoolExecutor
implements TaskTrackingExecutor, TaskTrackingExecutorDecorator.HookableExecutor {
final TaskTrackingExecutorDecorator taskTrackingDecorator;
public ScheduledTaskTrackingThreadPoolExecutor(int corePoolSize) {
super(corePoolSize);
taskTrackingDecorator = new TaskTrackingExecutorDecorator(this, corePoolSize);
}
@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));
}
/** Decorates {@code task} using {@link ScheduledExecution}. */
@Override
protected <V> ScheduledExecution<V> decorateTask(
Runnable task,
RunnableScheduledFuture<V> scheduledExecution
) {
return new ScheduledExecution<>(task, scheduledExecution);
}
/** Decorates {@code task} using {@link ScheduledExecution}. */
@Override
protected <V> ScheduledExecution<V> decorateTask(
Callable<V> task,
RunnableScheduledFuture<V> scheduledExecution
) {
return new ScheduledExecution<>(task, scheduledExecution);
}
/**
* Decorates a {@link RunnableScheduledFuture} to allow to obtain the original scheduled task.
*/
public static class ScheduledExecution<V> implements RunnableScheduledFuture<V> {
/** The original scheduled task. Either a {@link Runnable} or a {@link Callable}. */
public Object getTask() { return task; }
final Object task;
final RunnableScheduledFuture<V> wrappedScheduledItem;
public ScheduledExecution(Object task, RunnableScheduledFuture<V> scheduledItemToWrap) {
this.task = task;
this.wrappedScheduledItem = scheduledItemToWrap;
}
// only dumb delegations to wrappedScheduledItem below
@Override public boolean isPeriodic() {
return wrappedScheduledItem.isPeriodic();
}
@Override public void run() {
wrappedScheduledItem.run();
}
@Override public boolean cancel(boolean mayInterruptIfRunning) {
return wrappedScheduledItem.cancel(mayInterruptIfRunning);
}
@Override public boolean isCancelled() {
return wrappedScheduledItem.isCancelled();
}
@Override public boolean isDone() {
return wrappedScheduledItem.isDone();
}
@Override public V get() throws InterruptedException, ExecutionException {
return wrappedScheduledItem.get();
}
@Override public V get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException {
return wrappedScheduledItem.get(timeout, unit);
}
@Override public long getDelay(TimeUnit unit) {
return wrappedScheduledItem.getDelay(unit);
}
@Override public int compareTo(Delayed o) {
return wrappedScheduledItem.compareTo(o);
}
}
public ScheduledTaskTrackingThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory) {
super(corePoolSize, threadFactory);
taskTrackingDecorator = new TaskTrackingExecutorDecorator(this, corePoolSize);
}
public ScheduledTaskTrackingThreadPoolExecutor(
int corePoolSize,
RejectedExecutionHandler handler
) {
super(corePoolSize, handler);
taskTrackingDecorator = new TaskTrackingExecutorDecorator(this, corePoolSize);
}
public ScheduledTaskTrackingThreadPoolExecutor(
int corePoolSize,
ThreadFactory threadFactory,
RejectedExecutionHandler handler
) {
super(corePoolSize, threadFactory, handler);
taskTrackingDecorator = new TaskTrackingExecutorDecorator(this, corePoolSize);
}
}