001/**
002 * Logback: the reliable, generic, fast and flexible logging framework. Copyright (C) 1999-2015, QOS.ch. All rights
003 * reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under either the terms of the Eclipse Public License
006 * v1.0 as published by the Eclipse Foundation
007 *
008 * or (per the licensee's choosing)
009 *
010 * under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation.
011 */
012package ch.qos.logback.core.util;
013
014import java.lang.reflect.InvocationTargetException;
015import java.lang.reflect.Method;
016import java.util.concurrent.ExecutorService;
017import java.util.concurrent.Executors;
018import java.util.concurrent.ScheduledExecutorService;
019import java.util.concurrent.ScheduledThreadPoolExecutor;
020import java.util.concurrent.SynchronousQueue;
021import java.util.concurrent.ThreadFactory;
022import java.util.concurrent.ThreadPoolExecutor;
023import java.util.concurrent.TimeUnit;
024import java.util.concurrent.atomic.AtomicInteger;
025
026import ch.qos.logback.core.CoreConstants;
027
028/**
029 * Static utility methods for manipulating an {@link ExecutorService}.
030 *
031 * @author Carl Harris
032 * @author Mikhail Mazursky
033 */
034public class ExecutorServiceUtil {
035
036    private static final ThreadFactory THREAD_FACTORY_FOR_SCHEDULED_EXECUTION_SERVICE = new ThreadFactory() {
037
038        private final AtomicInteger threadNumber = new AtomicInteger(1);
039
040        private final ThreadFactory defaultFactory = makeThreadFactory();
041
042        /**
043         * A thread factory which may be a virtual thread factory the JDK supports it.
044         *
045         * @return
046         */
047        private ThreadFactory makeThreadFactory() {
048            return Executors.defaultThreadFactory();
049        }
050
051        @Override
052        public Thread newThread(Runnable r) {
053            Thread thread = defaultFactory.newThread(r);
054            if (!thread.isDaemon()) {
055                thread.setDaemon(true);
056            }
057            thread.setName("logback-" + threadNumber.getAndIncrement());
058            return thread;
059        }
060    };
061
062    static public ScheduledExecutorService newScheduledExecutorService() {
063        return new ScheduledThreadPoolExecutor(CoreConstants.SCHEDULED_EXECUTOR_POOL_SIZE,
064                THREAD_FACTORY_FOR_SCHEDULED_EXECUTION_SERVICE);
065    }
066
067    /**
068     * @deprecated replaced by {@link #newThreadPoolExecutor()}
069     */
070    static public ExecutorService newExecutorService() {
071        return newThreadPoolExecutor();
072    }
073
074    /**
075     * Creates an ThreadPoolExecutor suitable for use by logback components.
076     *
077     * @since 1.4.7
078     * @return ThreadPoolExecutor
079     */
080    static public ThreadPoolExecutor newThreadPoolExecutor() {
081        return new ThreadPoolExecutor(CoreConstants.CORE_POOL_SIZE, CoreConstants.MAX_POOL_SIZE, 0L,
082                TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(),
083                THREAD_FACTORY_FOR_SCHEDULED_EXECUTION_SERVICE);
084    }
085
086    /**
087     * Shuts down an executor service.
088     * <p>
089     *
090     * @param executorService the executor service to shut down
091     */
092    static public void shutdown(ExecutorService executorService) {
093        if (executorService != null) {
094            executorService.shutdownNow();
095        }
096    }
097
098    /**
099     * 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}