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