-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadPoolUtil.java
153 lines (130 loc) · 6.75 KB
/
ThreadPoolUtil.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
import java.util.*;
import java.util.concurrent.*;
/**
* Created with IntelliJ IDEA.
* User: WuYifei
* Date: 2017/2/8
* Time: 10:09
*/
public final class ThreadPoolUtil {
private static final Map<String, ExecutorService> poolMonitor = new HashMap<String, ExecutorService>();
private static final Object lock = new Object();
private static final CleanThread cleanThread = new CleanThread();
private static class CleanThread implements Runnable {
private List<Thread> threads = Collections.synchronizedList(new ArrayList<Thread>());
private void addThread(Thread thread) {
threads.add(thread);
}
public void run() {
for (Thread thread : threads) {
thread.interrupt();
}
}
}
public static abstract class CleanThreadPool implements Runnable {
protected ExecutorService executorService;
public CleanThreadPool(ExecutorService executorService) {
this.executorService = executorService;
}
}
public static class DefaultCleanThreadPool extends CleanThreadPool {
public DefaultCleanThreadPool(ExecutorService executorService) {
super(executorService);
}
public void run() {
executorService.shutdownNow();
}
}
public static ExecutorService newFixedExecutor(int nThread, String prefix) {
ExecutorService executorService = Executors.newFixedThreadPool(nThread, new SimpleThreadFactory(prefix));
Runtime.getRuntime().addShutdownHook(new Thread(new DefaultCleanThreadPool(executorService)));
String info = String.format("ThreadPool name : %s, fixed thread : %d, default clean ", prefix, nThread);
synchronized (lock) {
poolMonitor.put(info, executorService);
}
return executorService;
}
public static ExecutorService newFixedExecutor(int nThread, String prefix, CleanThreadPool cleanThreadPool) {
ExecutorService executorService = Executors.newFixedThreadPool(nThread, new SimpleThreadFactory(prefix));
Runtime.getRuntime().addShutdownHook(new Thread(cleanThreadPool));
String info = String.format("ThreadPool name : %s, fixed thread : %d, not default clean ", prefix, nThread);
synchronized (lock) {
poolMonitor.put(info, executorService);
}
return executorService;
}
public static ExecutorService newCachedExecutor(String prefix) {
ExecutorService executorService = new ThreadPoolExecutor(20, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(),
new SimpleThreadFactory(prefix));
Runtime.getRuntime().addShutdownHook(new Thread(new DefaultCleanThreadPool(executorService)));
String info = String.format("ThreadPool name : %s, cache thread , default clean ", prefix);
synchronized (lock) {
poolMonitor.put(info, executorService);
}
return executorService;
}
public static ExecutorService newCachedExecutor(String prefix, CleanThreadPool cleanThreadPool) {
ExecutorService executorService = new ThreadPoolExecutor(20, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(),
new SimpleThreadFactory(prefix));
Runtime.getRuntime().addShutdownHook(new Thread(cleanThreadPool));
String info = String.format("ThreadPool name : %s, cache thread , not default clean ", prefix);
synchronized (lock) {
poolMonitor.put(info, executorService);
}
return executorService;
}
public static ExecutorService newCachedExecutor(int corePoolSize, String prefix) {
ExecutorService executorService = new ThreadPoolExecutor(corePoolSize, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(),
new SimpleThreadFactory(prefix));
Runtime.getRuntime().addShutdownHook(new Thread(new DefaultCleanThreadPool(executorService)));
String info = String.format("ThreadPool name : %s, cache thread , default clean ", prefix);
synchronized (lock) {
poolMonitor.put(info, executorService);
}
return executorService;
}
public static ExecutorService newCachedExecutor(int corePoolSize, String prefix, CleanThreadPool cleanThreadPool) {
ExecutorService executorService = new ThreadPoolExecutor(corePoolSize, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(),
new SimpleThreadFactory(prefix));
Runtime.getRuntime().addShutdownHook(new Thread(cleanThreadPool));
String info = String.format("ThreadPool name : %s, cache thread , not default clean ", prefix);
synchronized (lock) {
poolMonitor.put(info, executorService);
}
return executorService;
}
public static ThreadPoolExecutor newThreadPoolExecutor(int corePoolSize, int maximumPoolSize, String prefix) {
ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new SimpleThreadFactory(prefix));
Runtime.getRuntime().addShutdownHook(new Thread(new DefaultCleanThreadPool(poolExecutor)));
String info = String.format("ThreadPool name : %s, pool thread %d core. %d max , default clean ", prefix, corePoolSize, maximumPoolSize);
synchronized (lock) {
poolMonitor.put(info, poolExecutor);
}
return poolExecutor;
}
public static ThreadPoolExecutor newThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, String prefix, CleanThreadPool cleanThreadPool) {
ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, new LinkedBlockingQueue<Runnable>(), new SimpleThreadFactory(prefix));
Runtime.getRuntime().addShutdownHook(new Thread(cleanThreadPool));
String info = String.format("ThreadPool name : %s, pool thread %d core. %d max. %d aliveTime. %s unit , default clean ", prefix, corePoolSize, maximumPoolSize, keepAliveTime, unit.name());
synchronized (lock) {
poolMonitor.put(info, poolExecutor);
}
return poolExecutor;
}
public static void registerShutdown(Thread thread) {
cleanThread.addThread(thread);
}
public static Map<String, ExecutorService> monitorThreadPool() {
synchronized (lock) {
return new HashMap<String, ExecutorService>(poolMonitor);
}
}
}