001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017
018package org.apache.logging.log4j.core.util;
019
020import java.util.concurrent.ThreadFactory;
021import java.util.concurrent.atomic.AtomicInteger;
022
023/**
024 * Creates {@link Log4jThread}s.
025 *
026 * @since 2.7
027 */
028public class Log4jThreadFactory implements ThreadFactory {
029
030    private static final String PREFIX = "TF-";
031
032    /**
033     * Creates a new daemon thread factory.
034     *
035     * @param threadFactoryName
036     *            The thread factory name.
037     * @return a new daemon thread factory.
038     */
039    public static Log4jThreadFactory createDaemonThreadFactory(final String threadFactoryName) {
040        return new Log4jThreadFactory(threadFactoryName, true, Thread.NORM_PRIORITY);
041    }
042
043    /**
044     * Creates a new thread factory.
045     *
046     * This is mainly used for tests. Production code should be very careful with creating
047     * non-daemon threads since those will block application shutdown
048     * (see https://issues.apache.org/jira/browse/LOG4J2-1748).
049     *
050     * @param threadFactoryName
051     *            The thread factory name.
052     * @return a new daemon thread factory.
053     */
054    public static Log4jThreadFactory createThreadFactory(final String threadFactoryName) {
055        return new Log4jThreadFactory(threadFactoryName, false, Thread.NORM_PRIORITY);
056    }
057
058    private static final AtomicInteger FACTORY_NUMBER = new AtomicInteger(1);
059    private static final AtomicInteger THREAD_NUMBER = new AtomicInteger(1);
060    private final boolean daemon;
061    private final ThreadGroup group;
062    private final int priority;
063    private final String threadNamePrefix;
064
065    /**
066     * Constructs an initialized thread factory.
067     *
068     * @param threadFactoryName
069     *            The thread factory name.
070     * @param daemon
071     *            Whether to create daemon threads.
072     * @param priority
073     *            The thread priority.
074     */
075    public Log4jThreadFactory(final String threadFactoryName, final boolean daemon, final int priority) {
076        this.threadNamePrefix = PREFIX + FACTORY_NUMBER.getAndIncrement() + "-" + threadFactoryName + "-";
077        this.daemon = daemon;
078        this.priority = priority;
079        final SecurityManager securityManager = System.getSecurityManager();
080        this.group = securityManager != null ? securityManager.getThreadGroup()
081                : Thread.currentThread().getThreadGroup();
082    }
083
084    @Override
085    public Thread newThread(final Runnable runnable) {
086        // Log4jThread prefixes names with "Log4j2-".
087        final Thread thread = new Log4jThread(group, runnable, threadNamePrefix + THREAD_NUMBER.getAndIncrement(), 0);
088        if (thread.isDaemon() != daemon) {
089            thread.setDaemon(daemon);
090        }
091        if (thread.getPriority() != priority) {
092            thread.setPriority(priority);
093        }
094        return thread;
095    }
096
097}