1
2
3
4
5
6
7
8
9
10
11
12 package ch.qos.logback.core.util;
13
14 import java.lang.reflect.InvocationTargetException;
15 import java.lang.reflect.Method;
16 import java.util.concurrent.ExecutorService;
17 import java.util.concurrent.Executors;
18 import java.util.concurrent.ScheduledExecutorService;
19 import java.util.concurrent.ScheduledThreadPoolExecutor;
20 import java.util.concurrent.SynchronousQueue;
21 import java.util.concurrent.ThreadFactory;
22 import java.util.concurrent.ThreadPoolExecutor;
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.atomic.AtomicInteger;
25
26 import ch.qos.logback.core.CoreConstants;
27
28
29
30
31
32
33
34 public class ExecutorServiceUtil {
35
36 private static final ThreadFactory THREAD_FACTORY_FOR_SCHEDULED_EXECUTION_SERVICE = new ThreadFactory() {
37
38 private final AtomicInteger threadNumber = new AtomicInteger(1);
39
40 private final ThreadFactory defaultFactory = makeThreadFactory();
41
42
43
44
45
46
47 private ThreadFactory makeThreadFactory() {
48 return Executors.defaultThreadFactory();
49 }
50
51 @Override
52 public Thread newThread(Runnable r) {
53 Thread thread = defaultFactory.newThread(r);
54 if (!thread.isDaemon()) {
55 thread.setDaemon(true);
56 }
57 thread.setName("logback-" + threadNumber.getAndIncrement());
58 return thread;
59 }
60 };
61
62 static public ScheduledExecutorService newScheduledExecutorService() {
63 return new ScheduledThreadPoolExecutor(CoreConstants.SCHEDULED_EXECUTOR_POOL_SIZE,
64 THREAD_FACTORY_FOR_SCHEDULED_EXECUTION_SERVICE);
65 }
66
67
68
69
70 static public ExecutorService newExecutorService() {
71 return newThreadPoolExecutor();
72 }
73
74
75
76
77
78
79
80 static public ThreadPoolExecutor newThreadPoolExecutor() {
81 return new ThreadPoolExecutor(CoreConstants.CORE_POOL_SIZE, CoreConstants.MAX_POOL_SIZE, 0L,
82 TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(),
83 THREAD_FACTORY_FOR_SCHEDULED_EXECUTION_SERVICE);
84 }
85
86
87
88
89
90
91
92 static public void shutdown(ExecutorService executorService) {
93 if (executorService != null) {
94 executorService.shutdownNow();
95 }
96 }
97
98
99
100
101
102
103
104 static public ExecutorService newAlternateThreadPoolExecutor() {
105 return newThreadPoolExecutor();
106 }
107 }