Java multithreading -- View thread stack information

Absrtact: This paper mainly introduces the method of viewing thread stack information.

Use the getallstacktrace() method of the Thread class

Method definition

You can see that the return value of getallstacktrace() method is a Map object, the key is an instance of Thread, and the value is an array of StackTraceElement instances:

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

Use

You can use this static method to cycle print on the page to get the stack situation at run time:

 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>

Page display

The following is displayed on the page:

 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)

Tags: PHP Java Tomcat Apache

Posted on Fri, 01 Nov 2019 05:39:49 -0700 by php noob