forked from alibaba/transmittable-thread-local
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TtlExecutors.java
293 lines (270 loc) · 12.4 KB
/
TtlExecutors.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package com.alibaba.ttl.threadpool;
import com.alibaba.ttl.TransmittableThreadLocal;
import com.alibaba.ttl.spi.TtlEnhanced;
import com.alibaba.ttl.spi.TtlWrapper;
import com.alibaba.ttl.threadpool.agent.TtlAgent;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.Comparator;
import java.util.concurrent.*;
/**
* Util methods for TTL wrapper of jdk executors.
*
* <ol>
* <li>wrap(factory)/check/unwrap methods for TTL wrapper of jdk executors({@link Executor}, {@link ExecutorService}, {@link ScheduledExecutorService}).</li>
* <li>wrap/unwrap/check methods to disable Inheritable for {@link ThreadFactory}.</li>
* <li>wrap/unwrap/check methods to {@code TtlRunnableUnwrapComparator} for {@link PriorityBlockingQueue}.</li>
* </ol>
* <p>
* <b><i>Note:</i></b>
* <ul>
* <li>all method is {@code null}-safe, when input {@code executor} parameter is {@code null}, return {@code null}.</li>
* <li>skip wrap/decoration thread pool/{@code executor}(aka. just return input {@code executor})
* when ttl agent is loaded, Or when input {@code executor} is already wrapped/decorated.</li>
* </ul>
*
* @author Jerry Lee (oldratlee at gmail dot com)
* @see Executor
* @see ExecutorService
* @see ScheduledExecutorService
* @see ThreadPoolExecutor
* @see ScheduledThreadPoolExecutor
* @see Executors
* @see java.util.concurrent.CompletionService
* @see java.util.concurrent.ExecutorCompletionService
* @see ThreadFactory
* @see Executors#defaultThreadFactory()
* @see PriorityBlockingQueue
* @see TtlForkJoinPoolHelper
* @since 0.9.0
*/
public final class TtlExecutors {
/**
* {@link TransmittableThreadLocal} Wrapper of {@link Executor},
* transmit the {@link TransmittableThreadLocal} from the task submit time of {@link Runnable}
* to the execution time of {@link Runnable}.
* <p>
* NOTE: sine v2.12.0 the idempotency of return wrapped Executor is changed to true,
* so the wrapped Executor can be cooperated with the usage of "Decorate Runnable and Callable".
* <p>
* About idempotency: if is idempotent,
* it's allowed to submit the {@link com.alibaba.ttl.TtlRunnable}/{@link com.alibaba.ttl.TtlCallable} to the wrapped Executor;
* otherwise throw {@link IllegalStateException}.
*
* @param executor input Executor
* @return wrapped Executor
* @see com.alibaba.ttl.TtlRunnable#get(Runnable, boolean, boolean)
* @see com.alibaba.ttl.TtlCallable#get(Callable, boolean, boolean)
*/
@Nullable
public static Executor getTtlExecutor(@Nullable Executor executor) {
if (TtlAgent.isTtlAgentLoaded() || null == executor || executor instanceof TtlEnhanced) {
return executor;
}
return new ExecutorTtlWrapper(executor, true);
}
/**
* {@link TransmittableThreadLocal} Wrapper of {@link ExecutorService},
* transmit the {@link TransmittableThreadLocal} from the task submit time of {@link Runnable} or {@link java.util.concurrent.Callable}
* to the execution time of {@link Runnable} or {@link java.util.concurrent.Callable}.
* <p>
* NOTE: sine v2.12.0 the idempotency of return wrapped ExecutorService is changed to true,
* so the wrapped ExecutorService can be cooperated with the usage of "Decorate Runnable and Callable".
* <p>
* About idempotency: if is idempotent,
* it's allowed to submit the {@link com.alibaba.ttl.TtlRunnable}/{@link com.alibaba.ttl.TtlCallable} to the wrapped ExecutorService;
* otherwise throw {@link IllegalStateException}.
*
* @param executorService input ExecutorService
* @return wrapped ExecutorService
* @see com.alibaba.ttl.TtlRunnable#get(Runnable, boolean, boolean)
* @see com.alibaba.ttl.TtlCallable#get(Callable, boolean, boolean)
*/
@Nullable
public static ExecutorService getTtlExecutorService(@Nullable ExecutorService executorService) {
if (TtlAgent.isTtlAgentLoaded() || executorService == null || executorService instanceof TtlEnhanced) {
return executorService;
}
return new ExecutorServiceTtlWrapper(executorService, true);
}
/**
* {@link TransmittableThreadLocal} Wrapper of {@link ScheduledExecutorService},
* transmit the {@link TransmittableThreadLocal} from the task submit time of {@link Runnable} or {@link java.util.concurrent.Callable}
* to the execution time of {@link Runnable} or {@link java.util.concurrent.Callable}.
* <p>
* NOTE: sine v2.12.0 the idempotency of return wrapped ScheduledExecutorService is changed to true,
* so the wrapped ScheduledExecutorService can be cooperated with the usage of "Decorate Runnable and Callable".
* <p>
* About idempotency: if is idempotent,
* it's allowed to submit the {@link com.alibaba.ttl.TtlRunnable}/{@link com.alibaba.ttl.TtlCallable} to the wrapped ScheduledExecutorService;
* otherwise throw {@link IllegalStateException}.
*
* @param scheduledExecutorService input scheduledExecutorService
* @return wrapped scheduledExecutorService
* @see com.alibaba.ttl.TtlRunnable#get(Runnable, boolean, boolean)
* @see com.alibaba.ttl.TtlCallable#get(Callable, boolean, boolean)
*/
@Nullable
public static ScheduledExecutorService getTtlScheduledExecutorService(@Nullable ScheduledExecutorService scheduledExecutorService) {
if (TtlAgent.isTtlAgentLoaded() || scheduledExecutorService == null || scheduledExecutorService instanceof TtlEnhanced) {
return scheduledExecutorService;
}
return new ScheduledExecutorServiceTtlWrapper(scheduledExecutorService, true);
}
/**
* check the executor is a TTL executor wrapper or not.
* <p>
* if the parameter executor is TTL wrapper, return {@code true}, otherwise {@code false}.
* <p>
* NOTE: if input executor is {@code null}, return {@code false}.
*
* @param executor input executor
* @param <T> Executor type
* @see #getTtlExecutor(Executor)
* @see #getTtlExecutorService(ExecutorService)
* @see #getTtlScheduledExecutorService(ScheduledExecutorService)
* @see #unwrap(Executor)
* @since 2.8.0
*/
public static <T extends Executor> boolean isTtlWrapper(@Nullable T executor) {
return executor instanceof TtlWrapper;
}
/**
* Unwrap TTL executor wrapper to the original/underneath one.
* <p>
* if the parameter executor is TTL wrapper, return the original/underneath executor;
* otherwise, just return the input parameter executor.
* <p>
* NOTE: if input executor is {@code null}, return {@code null}.
*
* @param executor input executor
* @param <T> Executor type
* @see #getTtlExecutor(Executor)
* @see #getTtlExecutorService(ExecutorService)
* @see #getTtlScheduledExecutorService(ScheduledExecutorService)
* @see #isTtlWrapper(Executor)
* @see com.alibaba.ttl.TtlUnwrap#unwrap(Object)
* @since 2.8.0
*/
@Nullable
@SuppressWarnings("unchecked")
public static <T extends Executor> T unwrap(@Nullable T executor) {
if (!isTtlWrapper(executor)) return executor;
return (T) ((ExecutorTtlWrapper) executor).unwrap();
}
/**
* Wrapper of {@link ThreadFactory}, disable inheritable.
*
* @param threadFactory input thread factory
* @see DisableInheritableThreadFactory
* @see TtlForkJoinPoolHelper#getDisableInheritableForkJoinWorkerThreadFactory
* @since 2.10.0
*/
@Nullable
public static ThreadFactory getDisableInheritableThreadFactory(@Nullable ThreadFactory threadFactory) {
if (threadFactory == null || isDisableInheritableThreadFactory(threadFactory)) return threadFactory;
return new DisableInheritableThreadFactoryWrapper(threadFactory);
}
/**
* Wrapper of {@link Executors#defaultThreadFactory()}, disable inheritable.
*
* @see #getDisableInheritableThreadFactory(ThreadFactory)
* @see TtlForkJoinPoolHelper#getDefaultDisableInheritableForkJoinWorkerThreadFactory()
* @since 2.10.0
*/
@NonNull
@SuppressWarnings("ConstantConditions")
public static ThreadFactory getDefaultDisableInheritableThreadFactory() {
return getDisableInheritableThreadFactory(Executors.defaultThreadFactory());
}
/**
* check the {@link ThreadFactory} is {@link DisableInheritableThreadFactory} or not.
*
* @see TtlForkJoinPoolHelper#getDisableInheritableForkJoinWorkerThreadFactory
* @see #getDefaultDisableInheritableThreadFactory()
* @see DisableInheritableThreadFactory
* @since 2.10.0
*/
public static boolean isDisableInheritableThreadFactory(@Nullable ThreadFactory threadFactory) {
return threadFactory instanceof DisableInheritableThreadFactory;
}
/**
* Unwrap {@link DisableInheritableThreadFactory} to the original/underneath one.
*
* @see #getDisableInheritableThreadFactory(ThreadFactory)
* @see #getDefaultDisableInheritableThreadFactory()
* @see #isDisableInheritableThreadFactory(ThreadFactory)
* @see TtlForkJoinPoolHelper#unwrap(ForkJoinPool.ForkJoinWorkerThreadFactory)
* @see DisableInheritableThreadFactory
* @see com.alibaba.ttl.TtlUnwrap#unwrap(Object)
* @since 2.10.0
*/
@Nullable
public static ThreadFactory unwrap(@Nullable ThreadFactory threadFactory) {
if (!isDisableInheritableThreadFactory(threadFactory)) return threadFactory;
return ((DisableInheritableThreadFactory) threadFactory).unwrap();
}
/**
* Wrapper of {@code Comparator<Runnable>} which unwrap {@link com.alibaba.ttl.TtlRunnable} before compare,
* aka {@code TtlRunnableUnwrapComparator}.
* <p>
* Prepared for {@code comparator} parameter of constructor
* {@link PriorityBlockingQueue#PriorityBlockingQueue(int, Comparator)}.
* <p>
* {@link PriorityBlockingQueue} can be used by constructor
* {@link ThreadPoolExecutor#ThreadPoolExecutor(int, int, long, java.util.concurrent.TimeUnit, java.util.concurrent.BlockingQueue)}.
*
* @param comparator input comparator
* @return wrapped comparator
* @see ThreadPoolExecutor
* @see ThreadPoolExecutor#ThreadPoolExecutor(int, int, long, java.util.concurrent.TimeUnit, java.util.concurrent.BlockingQueue)
* @see PriorityBlockingQueue
* @see PriorityBlockingQueue#PriorityBlockingQueue(int, Comparator)
* @since 2.12.3
*/
@Nullable
public static Comparator<Runnable> getTtlRunnableUnwrapComparator(@Nullable Comparator<Runnable> comparator) {
if (comparator == null || isTtlRunnableUnwrapComparator(comparator)) return comparator;
return new TtlUnwrapComparator<Runnable>(comparator);
}
@SuppressWarnings({"unchecked", "rawtypes"})
private static final Comparator INSTANCE = new TtlUnwrapComparator(ComparableComparator.INSTANCE);
/**
* {@code TtlRunnableUnwrapComparator} that compares {@link Comparable Comparable} objects.
*
* @see #getTtlRunnableUnwrapComparator(Comparator)
* @since 2.12.3
*/
@NonNull
@SuppressWarnings("unchecked")
public static Comparator<Runnable> getTtlRunnableUnwrapComparatorForComparableRunnable() {
return (Comparator<Runnable>) INSTANCE;
}
/**
* check the {@code Comparator<Runnable>} is a wrapper {@code TtlRunnableUnwrapComparator} or not.
*
* @see #getTtlRunnableUnwrapComparator(Comparator)
* @see #getTtlRunnableUnwrapComparatorForComparableRunnable()
* @since 2.12.3
*/
public static boolean isTtlRunnableUnwrapComparator(@Nullable Comparator<Runnable> comparator) {
return comparator instanceof TtlUnwrapComparator;
}
/**
* Unwrap {@code TtlRunnableUnwrapComparator} to the original/underneath {@code Comparator<Runnable>}.
*
* @see #getTtlRunnableUnwrapComparator(Comparator)
* @see #getTtlRunnableUnwrapComparatorForComparableRunnable()
* @see #isTtlRunnableUnwrapComparator(Comparator)
* @see com.alibaba.ttl.TtlUnwrap#unwrap(Object)
* @since 2.12.3
*/
@Nullable
public static Comparator<Runnable> unwrap(@Nullable Comparator<Runnable> comparator) {
if (!isTtlRunnableUnwrapComparator(comparator)) return comparator;
return ((TtlUnwrapComparator<Runnable>) comparator).unwrap();
}
private TtlExecutors() {
throw new InstantiationError("Must not instantiate this class");
}
}