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       * This is mainly used for tests. Production code should be very careful with creating
47       * non-daemon threads since those will block application shutdown
48       * (see https://issues.apache.org/jira/browse/LOG4J2-1748).
49       *
50       * @param threadFactoryName
51       *            The thread factory name.
52       * @return a new daemon thread factory.
53       */
54      public static Log4jThreadFactory createThreadFactory(final String threadFactoryName) {
55          return new Log4jThreadFactory(threadFactoryName, false, Thread.NORM_PRIORITY);
56      }
57  
58      private static final AtomicInteger FACTORY_NUMBER = new AtomicInteger(1);
59      private static final AtomicInteger THREAD_NUMBER = new AtomicInteger(1);
60      private final boolean daemon;
61      private final ThreadGroup group;
62      private final int priority;
63      private final String threadNamePrefix;
64  
65      /**
66       * Constructs an initialized thread factory.
67       *
68       * @param threadFactoryName
69       *            The thread factory name.
70       * @param daemon
71       *            Whether to create daemon threads.
72       * @param priority
73       *            The thread priority.
74       */
75      public Log4jThreadFactory(final String threadFactoryName, final boolean daemon, final int priority) {
76          this.threadNamePrefix = PREFIX + FACTORY_NUMBER.getAndIncrement() + "-" + threadFactoryName + "-";
77          this.daemon = daemon;
78          this.priority = priority;
79          final SecurityManager securityManager = System.getSecurityManager();
80          this.group = securityManager != null ? securityManager.getThreadGroup()
81                  : Thread.currentThread().getThreadGroup();
82      }
83  
84      @Override
85      public Thread newThread(final Runnable runnable) {
86          // Log4jThread prefixes names with "Log4j2-".
87          final Thread thread = new Log4jThread(group, runnable, threadNamePrefix + THREAD_NUMBER.getAndIncrement(), 0);
88          if (thread.isDaemon() != daemon) {
89              thread.setDaemon(daemon);
90          }
91          if (thread.getPriority() != priority) {
92              thread.setPriority(priority);
93          }
94          return thread;
95      }
96  
97  }