View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  
18  package org.apache.logging.log4j.core.util;
19  
20  import java.util.concurrent.ThreadFactory;
21  import java.util.concurrent.atomic.AtomicInteger;
22  
23  /**
24   * Creates {@link Log4jThread}s.
25   * 
26   * @since 2.7
27   */
28  public class Log4jThreadFactory implements ThreadFactory {
29  
30      private static final String PREFIX = "TF-";
31  
32      /**
33       * Creates a new daemon thread factory.
34       * 
35       * @param threadFactoryName
36       *            The thread factory name.
37       * @return a new daemon thread factory.
38       */
39      public static Log4jThreadFactory createDaemonThreadFactory(final String threadFactoryName) {
40          return new Log4jThreadFactory(threadFactoryName, true, Thread.NORM_PRIORITY);
41      }
42  
43      /**
44       * Creates a new thread factory.
45       * 
46       * @param threadFactoryName
47       *            The thread factory name.
48       * @return a new daemon thread factory.
49       */
50      public static Log4jThreadFactory createThreadFactory(final String threadFactoryName) {
51          return new Log4jThreadFactory(threadFactoryName, false, Thread.NORM_PRIORITY);
52      }
53  
54      private static final AtomicInteger FACTORY_NUMBER = new AtomicInteger(1);
55      private static final AtomicInteger THREAD_NUMBER = new AtomicInteger(1);
56      private final boolean daemon;
57      private final ThreadGroup group;
58      private final int priority;
59      private final String threadNamePrefix;
60  
61      /**
62       * Constructs an initialized thread factory.
63       * 
64       * @param threadFactoryName
65       *            The thread factory name.
66       * @param daemon
67       *            Whether to create daemon threads.
68       * @param priority
69       *            The thread priority.
70       */
71      public Log4jThreadFactory(final String threadFactoryName, final boolean daemon, final int priority) {
72          this.threadNamePrefix = PREFIX + FACTORY_NUMBER.getAndIncrement() + "-" + threadFactoryName + "-";
73          this.daemon = daemon;
74          this.priority = priority;
75          final SecurityManager securityManager = System.getSecurityManager();
76          this.group = securityManager != null ? securityManager.getThreadGroup()
77                  : Thread.currentThread().getThreadGroup();
78      }
79  
80      @Override
81      public Thread newThread(final Runnable runnable) {
82          // Log4jThread prefixes names with "Log4j2-".
83          final Thread thread = new Log4jThread(group, runnable, threadNamePrefix + THREAD_NUMBER.getAndIncrement(), 0);
84          if (thread.isDaemon() != daemon) {
85              thread.setDaemon(daemon);
86          }
87          if (thread.getPriority() != priority) {
88              thread.setPriority(priority);
89          }
90          return thread;
91      }
92  
93  }