1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.util;
15
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 = new ThreadFactory() {
37
38 private final ThreadFactory defaultFactory = Executors.defaultThreadFactory();
39 private final AtomicInteger threadNumber = new AtomicInteger(1);
40
41 public Thread newThread(Runnable r) {
42 Thread thread = defaultFactory.newThread(r);
43 if (!thread.isDaemon()) {
44 thread.setDaemon(true);
45 }
46 thread.setName("logback-" + threadNumber.getAndIncrement());
47 return thread;
48 }
49 };
50
51 static public ScheduledExecutorService newScheduledExecutorService() {
52 return new ScheduledThreadPoolExecutor(CoreConstants.SCHEDULED_EXECUTOR_POOL_SIZE, THREAD_FACTORY);
53 }
54
55
56
57
58
59
60 static public ExecutorService newExecutorService() {
61 return new ThreadPoolExecutor(CoreConstants.CORE_POOL_SIZE, CoreConstants.MAX_POOL_SIZE, 0L,
62 TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(), THREAD_FACTORY);
63 }
64
65
66
67
68
69
70
71 static public void shutdown(ExecutorService executorService) {
72 executorService.shutdownNow();
73 }
74
75 }