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.atomic.AtomicLong;
21  
22  /**
23   * Prefixes thread names with {@code "Log4j2-"}.
24   */
25  public class Log4jThread extends Thread {
26  
27      static final String PREFIX = "Log4j2-";
28  
29      private static final AtomicLong threadInitNumber = new AtomicLong();
30  
31      private static long nextThreadNum() {
32          return threadInitNumber.getAndIncrement();
33      }
34  
35      private static String toThreadName(final Object name) {
36          return PREFIX + name;
37      }
38  
39      public Log4jThread() {
40          super(toThreadName(nextThreadNum()));
41      }
42  
43      public Log4jThread(final Runnable target) {
44          super(target, toThreadName(nextThreadNum()));
45      }
46  
47      public Log4jThread(final Runnable target, final String name) {
48          super(target, toThreadName(name));
49      }
50  
51      public Log4jThread(final String name) {
52          super(toThreadName(name));
53      }
54  
55      public Log4jThread(final ThreadGroup group, final Runnable target) {
56          super(group, target, toThreadName(nextThreadNum()));
57      }
58  
59      public Log4jThread(final ThreadGroup group, final Runnable target, final String name) {
60          super(group, target, toThreadName(name));
61      }
62  
63      public Log4jThread(final ThreadGroup group, final Runnable target, final String name, final long stackSize) {
64          super(group, target, toThreadName(name), stackSize);
65      }
66  
67      public Log4jThread(final ThreadGroup group, final String name) {
68          super(group, toThreadName(name));
69      }
70  
71  }