1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import java.util.Date;
18  import java.util.Map;
19  
20  /**
21   * <a href="ThreadUtil.java.html"><b><i>View Source</i></b></a>
22   *
23   * @author Tina Tian
24   * @author Shuyang Zhou
25   */
26  public class ThreadUtil {
27  
28      public static Thread[] getThreads() {
29          Thread currentThread = Thread.currentThread();
30  
31          ThreadGroup threadGroup = currentThread.getThreadGroup( );
32  
33          while (threadGroup.getParent() != null) {
34              threadGroup = threadGroup.getParent();
35          }
36  
37          int threadCountGuess = threadGroup.activeCount();
38  
39          Thread[] threads = new Thread[threadCountGuess];
40  
41          int threadCountActual = threadGroup.enumerate(threads);
42  
43          while (threadCountActual == threadCountGuess) {
44              threadCountGuess *= 2;
45  
46              threads = new Thread[threadCountGuess];
47  
48              threadCountActual = threadGroup.enumerate(threads);
49          }
50  
51          return threads;
52      }
53  
54      public static String threadDump() {
55          String jvm =
56              System.getProperty("java.vm.name") + " " +
57                  System.getProperty("java.vm.version");
58  
59          StringBundler sb = new StringBundler(
60              "Full thread dump of " + jvm + " on " + String.valueOf(new Date()) +
61                  "\n\n");
62  
63          Map<Thread, StackTraceElement[]> stackTraces =
64              Thread.getAllStackTraces();
65  
66          for (Map.Entry<Thread, StackTraceElement[]> entry :
67                  stackTraces.entrySet()) {
68  
69              Thread thread = entry.getKey();
70              StackTraceElement[] elements = entry.getValue();
71  
72              sb.append(StringPool.QUOTE);
73              sb.append(thread.getName());
74              sb.append(StringPool.QUOTE);
75  
76              if (thread.getThreadGroup() != null) {
77                  sb.append(StringPool.SPACE);
78                  sb.append(StringPool.OPEN_PARENTHESIS);
79                  sb.append(thread.getThreadGroup().getName());
80                  sb.append(StringPool.CLOSE_PARENTHESIS);
81              }
82  
83              sb.append(", priority=");
84              sb.append(thread.getPriority());
85              sb.append(", id=");
86              sb.append(thread.getId());
87              sb.append(", state=");
88              sb.append(thread.getState());
89              sb.append("\n");
90  
91              for (int i = 0; i < elements.length; i++) {
92                  sb.append("\t");
93                  sb.append(elements[i]);
94                  sb.append("\n");
95              }
96  
97              sb.append("\n");
98          }
99  
100         return sb.toString();
101     }
102 
103 }