/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache license, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the license for the specific language governing permissions and
 * limitations under the license.
 */

package org.apache.logging.log4j.core.util;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Creates {@link Log4jThread}s.
 * 
 * @since 2.7
 */
public class Log4jThreadFactory implements ThreadFactory {

    private static final String PREFIX = Log4jThread.PREFIX + "TF-";

    /**
     * Creates a new daemon thread factory.
     * 
     * @param threadNamePrefix
     *            The thread name prefix.
     * @return a new daemon thread factory.
     */
    public static Log4jThreadFactory createDaemonThreadFactory(final String threadNamePrefix) {
        return new Log4jThreadFactory(threadNamePrefix, true, Thread.NORM_PRIORITY);
    }

    /**
     * Creates a new thread factory.
     * 
     * @param threadNamePrefix
     *            The thread name prefix.
     * @return a new daemon thread factory.
     */
    public static Log4jThreadFactory createThreadFactory(final String threadNamePrefix) {
        return new Log4jThreadFactory(threadNamePrefix, false, Thread.NORM_PRIORITY);
    }

    private static final AtomicInteger FACTORY_NUMBER = new AtomicInteger(1);
    private static final AtomicInteger THREAD_NUMBER = new AtomicInteger(1);
    private final boolean daemon;
    private final ThreadGroup group;
    private final int priority;
    private final String threadNamePrefix;

    /**
     * Constructs a thread factory with default settings.
     */
    public Log4jThreadFactory() {
        this("thread", false, Thread.NORM_PRIORITY);
    }

    /**
     * Constructs an initialized thread factory.
     * 
     * @param threadNamePrefix
     *            The thread name prefix.
     * @param daemon
     *            Whether to create daemon threads.
     * @param priority
     *            The thread priority.
     */
    public Log4jThreadFactory(final String threadNamePrefix, final boolean daemon, final int priority) {
        this.threadNamePrefix = PREFIX + FACTORY_NUMBER.getAndIncrement() + "-" + threadNamePrefix + "-";
        this.daemon = daemon;
        this.priority = priority;
        final SecurityManager securityManager = System.getSecurityManager();
        this.group = securityManager != null ? securityManager.getThreadGroup()
                : Thread.currentThread().getThreadGroup();
    }

    @Override
    public Thread newThread(final Runnable runnable) {
        final Thread thread = new Log4jThread(group, runnable, threadNamePrefix + THREAD_NUMBER.getAndIncrement(), 0);
        if (thread.isDaemon() != daemon) {
            thread.setDaemon(daemon);
        }
        if (thread.getPriority() != priority) {
            thread.setPriority(priority);
        }
        return thread;
    }

}

@@ -1097,29 +1097,8 @@
 IX =
- Log4jThread.PREFIX +
  %22TF
@@ -1178,34 +1178,35 @@
 param thread
-NamePrefix
+FactoryName
 %0A     *     
@@ -1219,35 +1219,36 @@
  The thread 
-name prefix
+factory name
 .%0A     * @re
@@ -1367,34 +1367,35 @@
 tring thread
-NamePrefix
+FactoryName
 ) %7B%0A        
@@ -1430,26 +1430,27 @@
 y(thread
-NamePrefix
+FactoryName
 , true, 
@@ -1548,34 +1548,35 @@
 param thread
-NamePrefix
+FactoryName
 %0A     *     
@@ -1589,35 +1589,36 @@
  The thread 
-name prefix
+factory name
 .%0A     * @re
@@ -1735,26 +1735,27 @@
 g thread
-NamePrefix
+FactoryName
 ) %7B%0A    
@@ -1794,26 +1794,27 @@
 y(thread
-NamePrefix
+FactoryName
 , false,
@@ -2150,176 +2150,8 @@
 x;%0A%0A
-    /**%0A     * Constructs a thread factory with default settings.%0A     */%0A    public Log4jThreadFactory() %7B%0A        this(%22thread%22, false, Thread.NORM_PRIORITY);%0A    %7D%0A%0A
     
@@ -2231,26 +2231,27 @@
 m thread
-NamePrefix
+FactoryName
 %0A     * 
@@ -2276,19 +2276,20 @@
 ead 
-name prefix
+factory name
 .%0A  
@@ -2478,26 +2478,27 @@
 g thread
-NamePrefix
+FactoryName
 , final 
@@ -2623,26 +2623,27 @@
 + thread
-NamePrefix
+FactoryName
  + %22-%22;%0A
@@ -2994,24 +2994,78 @@
 runnable) %7B%0A
+        // Log4jThread prefixes names with %22Log4j2-%22.%0A
         fina

Clean up thread naming.