不知觉间工作已有一年了,闲下来的时候总会思考下,作为一名Java程序员,不能一直停留在开发业务使用框架上面。老话说得好,机会是留给有准备的人的,因此,开始计划看一些Java底层一点的东西,尝试开始在学习的过程中写博客,希望和大家一起交流学习。
写在前面: 线程堆栈应该是多线程类应用程序非功能问题定位的最有效手段,可以说是杀手锏。线程堆栈最擅长与分析如下类型问题:
- 系统无缘无故CPU过高。
- 系统挂起,无响应。
- 系统运行越来越慢。
- 性能瓶颈(如无法充分利用CPU等)
- 线程死锁、死循环,饿死等。
- 由于线程数量太多导致系统失败(如无法创建线程等)。
如何解读线程堆栈
如下面一段Java源代码程序:
package org.ccgogoing.study.stacktrace;
/**
* @Author: LuoChong400
* @Description: 测试线程
* @Date: Create in 07:27 PM 2017/12/08 */ public class MyTest { Object obj1 = new Object(); Object obj2 = new Object(); public void fun1() { synchronized (obj1) { fun2(); } } public void fun2() { synchronized (obj2) { while (true) { //为了打印堆栈,该函数堆栈分析不退出 System.out.print(""); } } } public static void main(String[] args) { MyTest aa = new MyTest(); aa.fun1(); } }
在Idea 中运行该程序,然后按下CTRL+BREAK键,打印出线程堆栈信息如下:
Full thread dump Java HotSpot(TM) 64-Bit Server VM (24.79-b02 mixed mode):
"Service Thread" daemon prio=6 tid=0x000000000c53b000 nid=0xca58 runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread1" daemon prio=10 tid=0x000000000c516000 nid=0xd390 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread0" daemon prio=10 tid=0x000000000c515000 nid=0xcbac waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Monitor Ctrl-Break" daemon prio=6 tid=0x000000000c514000 nid=0xd148 runnable [0x000000000caee000] java.lang.Thread.State: RUNNABLE at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.read(SocketInputStream.java:152) at java.net.SocketInputStream.read(SocketInputStream.java:122) at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283) at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325) at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177) - locked <0x00000000d7858b50> (a java.io.InputStreamReader) at java.io.InputStreamReader.read(InputStreamReader.java:184) at java.io.BufferedReader.fill(BufferedReader.java:154) at java.io.BufferedReader.readLine(BufferedReader.java:317) - locked <0x00000000d7858b50> (a java.io.InputStreamReader) at java.io.BufferedReader.readLine(BufferedReader.java:382) at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:64) "Attach Listener" daemon prio=10 tid=0x000000000ad4a000 nid=0xd24c runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Signal Dispatcher" daemon prio=10 tid=0x000000000c1a8800 nid=0xd200 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Finalizer" daemon prio=8 tid=0x000000000ace6000 nid=0xcd74 in Object.wait() [0x000000000c13f000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x00000000d7284858> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135) - locked <0x00000000d7284858> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151) at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209) "Reference Handler" daemon prio=10 tid=0x000000000ace4800 nid=0xce34 in Object.wait() [0x000000000bf4f000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x00000000d7284470> (a java.lang.ref.Reference$Lock) at java.lang.Object.wait(Object.java:503) at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:133) - locked <0x00000000d7284470> (a java.lang.ref.Reference$Lock) "main" prio=6 tid=0x000000000238e800 nid=0xc940 runnable [0x00000000027af000] java.lang.Thread.State: RUNNABLE at org.ccgogoing.study.stacktrace.MyTest.fun2(MyTest.java:22) - locked <0x00000000d77d50c8> (a java.lang.Object) at org.ccgogoing.study.stacktrace.MyTest.fun1(MyTest.java:15) - locked <0x00000000d77d50b8> (a java.lang.Object) at org.ccgogoing.study.stacktrace.MyTest.main(MyTest.java:29) "VM Thread" prio=10 tid=0x000000000ace1000 nid=0xd0a8 runnable "GC task thread#0 (ParallelGC)" prio=6 tid=0x00000000023a4000 nid=0xd398 runnable "GC task thread#1 (ParallelGC)" prio=6 tid=0x00000000023a5800 nid=