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  package org.apache.logging.log4j.message;
18  
19  import org.apache.logging.log4j.util.Chars;
20  import org.apache.logging.log4j.util.StringBuilders;
21  
22  /**
23   * Generates information about the current Thread. Used internally by ThreadDumpMessage.
24   */
25  class BasicThreadInformation implements ThreadInformation {
26      private static final int HASH_SHIFT = 32;
27      private static final int HASH_MULTIPLIER = 31;
28      private final long id;
29      private final String name;
30      private final String longName;
31      private final Thread.State state;
32      private final int priority;
33      private final boolean isAlive;
34      private final boolean isDaemon;
35      private final String threadGroupName;
36  
37      /**
38       * The Constructor.
39       * @param thread The Thread to capture.
40       */
41      public BasicThreadInformation(final Thread thread) {
42          this.id = thread.getId();
43          this.name = thread.getName();
44          this.longName = thread.toString();
45          this.state = thread.getState();
46          this.priority = thread.getPriority();
47          this.isAlive = thread.isAlive();
48          this.isDaemon = thread.isDaemon();
49          final ThreadGroup group = thread.getThreadGroup();
50          threadGroupName = group == null ? null : group.getName();
51      }
52  
53      @Override
54      public boolean equals(final Object o) {
55          if (this == o) {
56              return true;
57          }
58          if (o == null || getClass() != o.getClass()) {
59              return false;
60          }
61  
62          final BasicThreadInformation that = (BasicThreadInformation) o;
63  
64          if (id != that.id) {
65              return false;
66          }
67          if (name != null ? !name.equals(that.name) : that.name != null) {
68              return false;
69          }
70  
71          return true;
72      }
73  
74      @Override
75      public int hashCode() {
76          int result = (int) (id ^ (id >>> HASH_SHIFT));
77          result = HASH_MULTIPLIER * result + (name != null ? name.hashCode() : 0);
78          return result;
79      }
80  
81      /**
82       * Print the thread information.
83       * @param sb The StringBuilder.
84       */
85      @Override
86      public void printThreadInfo(final StringBuilder sb) {
87          StringBuilders.appendDqValue(sb, name).append(Chars.SPACE);
88          if (isDaemon) {
89              sb.append("daemon ");
90          }
91          sb.append("prio=").append(priority).append(" tid=").append(id).append(' ');
92          if (threadGroupName != null) {
93              StringBuilders.appendKeyDqValue(sb, "group", threadGroupName);
94          }
95          sb.append('\n');
96          sb.append("\tThread state: ").append(state.name()).append('\n');
97      }
98  
99      /**
100      * Format the StackTraceElements.
101      * @param sb The StringBuilder.
102      * @param trace The stack trace element array to format.
103      */
104     @Override
105     public void printStack(final StringBuilder sb, final StackTraceElement[] trace) {
106         for (final StackTraceElement element : trace) {
107             sb.append("\tat ").append(element).append('\n');
108         }
109     }
110 }