View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework. Copyright (C) 1999-2015, QOS.ch. All rights
3    * reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under either the terms of the Eclipse Public License
6    * v1.0 as published by the Eclipse Foundation
7    *
8    * or (per the licensee's choosing)
9    *
10   * under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation.
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   * Static utility methods for manipulating an {@link ExecutorService}.
30   *
31   * @author Carl Harris
32   * @author Mikhail Mazursky
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           * A thread factory which may be a virtual thread factory the JDK supports it.
44           *
45           * @return
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       * @deprecated replaced by {@link #newThreadPoolExecutor()}
69       */
70      static public ExecutorService newExecutorService() {
71          return newThreadPoolExecutor();
72      }
73  
74      /**
75       * Creates an ThreadPoolExecutor suitable for use by logback components.
76       *
77       * @since 1.4.7
78       * @return ThreadPoolExecutor
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       * Shuts down an executor service.
88       * <p>
89       *
90       * @param executorService the executor service to shut down
91       */
92      static public void shutdown(ExecutorService executorService) {
93          if (executorService != null) {
94              executorService.shutdownNow();
95          }
96      }
97  
98      /**
99       * An alternate implementation of {@linl #newThreadPoolExecutor} which returns a virtual thread per task executor when
100      * available.
101      *
102      * @since 1.3.12/1.4.12
103      */
104     static public ExecutorService newAlternateThreadPoolExecutor() {
105         return newThreadPoolExecutor();
106     }
107 }