Java多线程——查看线程堆栈信息

Java多线程——查看线程堆栈信息

摘要:本文主要介绍了查看线程堆栈信息的方法。

使用Thread类的getAllStackTraces()方法

方法定义

可以看到getAllStackTraces()方法的返回值是一个Map对象,key是Thread的实例,value是一个StackTraceElement实例数组:

1 public static Map<Thread, StackTraceElement[]> getAllStackTraces()

使用

可以使用这个静态方法在页面上循环打印,从而获取到运行时的堆栈情况:

 1 <body>
 2     <%
 3     StringBuilder sb = new StringBuilder();
 4     Map<Thread, StackTraceElement[]> ts = Thread.getAllStackTraces();
 5     for (Thread t : ts.keySet()) {
 6         StackTraceElement[] s = ts.get(t);
 7         sb.append(t.getName() + ":" + t.getId()).append("<br>");
 8         for (StackTraceElement e : s) {
 9             sb.append("&emsp;").append(e).append("<br>");
10         }
11     }
12     out.print(sb.toString());
13     %>
14 </body>

页面显示

在页面上显示如下:

 1 Reference Handler:2
 2     java.lang.Object.wait(Native Method)
 3     java.lang.Object.wait(Object.java:502)
 4     java.lang.ref.Reference.tryHandlePending(Reference.java:191)
 5     java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
 6 Signal Dispatcher:4
 7 SockJS-5:40
 8     sun.misc.Unsafe.park(Native Method)
 9     java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
10     java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
11     java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1088)
12     java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
13     java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
14     java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
15     java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
16     java.lang.Thread.run(Thread.java:745)
17 ContainerBackgroundProcessor[StandardEngine[Tomcat]]:27
18     java.lang.Thread.sleep(Native Method)
19     org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1368)
20     java.lang.Thread.run(Thread.java:745)
21 C3P0PooledConnectionPoolManager[identityToken->1hge7sxa31d0rqyw19bxvtp|1e774dd6]-AdminTaskTimer:22
22     java.lang.Object.wait(Native Method)
23     java.util.TimerThread.mainLoop(Timer.java:552)
24     java.util.TimerThread.run(Timer.java:505)
25 SockJS-4:39
26     sun.misc.Unsafe.park(Native Method)
27     java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
28     java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
29     java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1088)
30     java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
31     java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
32     java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
33     java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
34     java.lang.Thread.run(Thread.java:745)
35 C3P0PooledConnectionPoolManager[identityToken->1hge7sxa31d0rqyw19bxvtp|1084583d]-HelperThread-#2:63
36     java.lang.Object.wait(Native Method)
37     com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:683)
点赞