forked from alibaba/transmittable-thread-local
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MtContextExecutors.java
52 lines (47 loc) · 2.21 KB
/
MtContextExecutors.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
package com.alibaba.mtc.threadpool;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
/**
* Factory Utils for getting MtContext Wrapper of jdk executors.
*
* @author ding.lid
* @since 0.9.0
*/
public final class MtContextExecutors {
/**
* {@link com.alibaba.mtc.MtContextThreadLocal} Wrapper of {@link Executor},
* transmit the {@link com.alibaba.mtc.MtContextThreadLocal} from the task submit time of {@link Runnable}
* to the execution time of {@link Runnable}.
*/
public static Executor getMtcExecutor(Executor executor) {
if (null == executor || executor instanceof ExecutorMtcWrapper) {
return executor;
}
return new ExecutorMtcWrapper(executor);
}
/**
* {@link com.alibaba.mtc.MtContextThreadLocal} Wrapper of {@link ExecutorService},
* transmit the {@link com.alibaba.mtc.MtContextThreadLocal} 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}.
*/
public static ExecutorService getMtcExecutorService(ExecutorService executorService) {
if (executorService == null || executorService instanceof ExecutorServiceMtcWrapper) {
return executorService;
}
return new ExecutorServiceMtcWrapper(executorService);
}
/**
* {@link com.alibaba.mtc.MtContextThreadLocal} Wrapper of {@link ScheduledExecutorService},
* transmit the {@link com.alibaba.mtc.MtContextThreadLocal } 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}.
*/
public static ScheduledExecutorService getMtcScheduledExecutorService(ScheduledExecutorService scheduledExecutorService) {
if (scheduledExecutorService == null || scheduledExecutorService instanceof ScheduledExecutorServiceMtcWrapper) {
return scheduledExecutorService;
}
return new ScheduledExecutorServiceMtcWrapper(scheduledExecutorService);
}
private MtContextExecutors() {
}
}