欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-04-09 19:41:32
Java多线程——查看线程堆栈信息 摘要:本文主要介绍了查看线程堆栈信息的方法。 使用Thread类的getAllStackTraces()方法 方法定义 可以看到getAllStackTraces()方法的返回值是一个Map对象,key是Thread的实例,value是一个StackTraceEl ......

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)