不知覺間工做已有一年了,閒下來的時候總會思考下,做爲一名Java程序員,不能一直停留在開發業務使用框架上面。老話說得好,機會是留給有準備的人的,所以,開始計劃看一些Java底層一點的東西,嘗試開始在學習的過程當中寫博客,但願和你們一塊兒交流學習。
寫在前面: 線程堆棧應該是多線程類應用程序非功能問題定位的最有效手段,能夠說是殺手鐗。線程堆棧最擅長與分析以下類型問題:java
以下面一段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=0xcc20 runnable "GC task thread#2 (ParallelGC)" prio=6 tid=0x00000000023a7000 nid=0xb914 runnable "GC task thread#3 (ParallelGC)" prio=6 tid=0x00000000023a9000 nid=0xd088 runnable "VM Periodic Task Thread" prio=10 tid=0x000000000c53f000 nid=0xc1b4 waiting on condition JNI global references: 138 Heap PSYoungGen total 36864K, used 6376K [0x00000000d7280000, 0x00000000d9b80000, 0x0000000100000000) eden space 31744K, 20% used [0x00000000d7280000,0x00000000d78ba0d0,0x00000000d9180000) from space 5120K, 0% used [0x00000000d9680000,0x00000000d9680000,0x00000000d9b80000) to space 5120K, 0% used [0x00000000d9180000,0x00000000d9180000,0x00000000d9680000) ParOldGen total 83456K, used 0K [0x0000000085800000, 0x000000008a980000, 0x00000000d7280000) object space 83456K, 0% used [0x0000000085800000,0x0000000085800000,0x000000008a980000) PSPermGen total 21504K, used 3300K [0x0000000080600000, 0x0000000081b00000, 0x0000000085800000) object space 21504K, 15% used [0x0000000080600000,0x0000000080939290,0x0000000081b00000)
在上面這段堆棧輸出中,能夠看到有不少後臺線程和main線程,其中只有main線程屬於Java用戶線程,其餘幾個都是虛擬機自動建立的,咱們分析的過程當中,只關心用戶線程便可。網絡
從上面的main線程中能夠很直觀的看到當前線程的調用上下文,其中一個線程的某一層調用含義以下:多線程
at MyTest.fun1(MyTest.java:15) | | | | | | | +-----當前正在調用的函數所在的源代碼文件的行號 | | +------------當前正在調用的函數所在的源代碼文件 | +---------------------當前正在調用的方法名 +---------------------------當前正在調用的類名
另外,堆棧中有:- locked <0x00000000d77d50b8> (a java.lang.Object)
語句,表示該線程已經佔有柯鎖<0x00000000d77d50b8>,尖括號中表示鎖ID,這個事系統自動產生的,咱們只須要知道每次打印的堆棧,同一個ID表示是同一個鎖便可。每個線程堆棧的第一行含義以下:app
"main" prio=1 tid=0x000000000238e800 nid=0xc940 runnable [0x00000000027af000] | | | | | | | | | | | +--線程佔用內存地址 | | | | +-----------線程的狀態 | | | +----線程對應的本地線程id號 | | +-------------------線程id | +--------------------------線程優先級 +-------------------------------線程名稱 其中須要說明的是,線程對應的本地線程id號,是指Java線程所對應的虛擬機中的本地線程。因爲Java是解析型語言,執行的實體是Java虛擬機,所以Java語言中的線程是依附於虛擬機中的本地線程來運行的,其實是本地線程在執行Java線程代碼。
從上面的線程堆棧看,線程堆棧中包含的直接信息爲:線程的個數,每一個線程調用的方法堆棧,當前鎖的狀態。線程的個數能夠直接數出來;線程調用的方法堆棧,從下向上看,即表示當前的線程調用了哪一個類上的哪一個方法。而鎖得狀態看起來稍微有一點技巧。與鎖相關的信息以下:框架
藉助線程堆棧,能夠分析不少類型的問題,CPU的消耗分析便是線程堆棧分析的一個重要內容;socket
處於TIMED_WAITING、WAITING狀態的線程必定不消耗CPU。處於RUNNABLE的線程,要結合當前代碼的性質判斷,是否消耗CPU。函數
線程堆棧在定位以下類型的問題上很是有幫助:性能
死鎖的概念就不作過多解釋了,不明白的能夠去網上查查;
兩個或超過兩個線程由於環路的鎖依賴關係而造成的鎖環,就造成了真正的死鎖,以下爲死鎖喉打印的堆棧:
Found one Java-level deadlock: ============================= "org.ccgogoing.study.stacktrace.deadlock.TestThread2": waiting to lock monitor 0x000000000a9ad118 (object 0x00000000d77363d0, a java.lang.Object), which is held by "org.ccgogoing.study.stacktrace.deadlock.TestThread1" "org.ccgogoing.study.stacktrace.deadlock.TestThread1": waiting to lock monitor 0x000000000a9abc78 (object 0x00000000d77363e0, a java.lang.Object), which is held by "org.ccgogoing.study.stacktrace.deadlock.TestThread2" Java stack information for the threads listed above: =================================================== "org.ccgogoing.study.stacktrace.deadlock.TestThread2": at org.ccgogoing.study.stacktrace.deadlock.TestThread2.fun(TestThread2.java:35) - waiting to lock <0x00000000d77363d0> (a java.lang.Object) - locked <0x00000000d77363e0> (a java.lang.Object) at org.ccgogoing.study.stacktrace.deadlock.TestThread2.run(TestThread2.java:22) "org.ccgogoing.study.stacktrace.deadlock.TestThread1": at org.ccgogoing.study.stacktrace.deadlock.TestThread1.fun(TestThread1.java:33) - waiting to lock <0x00000000d77363e0> (a java.lang.Object) - locked <0x00000000d77363d0> (a java.lang.Object) at org.ccgogoing.study.stacktrace.deadlock.TestThread1.run(TestThread1.java:20) Found 1 deadlock.
從打印的堆棧中咱們能看到"Found one Java-level deadlock:",即若是存在死鎖狀況,堆棧中會直接給出死鎖的分析結果.
當一組Java線程發生死鎖的時候,那麼意味着Game Over,這些線程永遠得被掛在那裏了,永遠不可能繼續運行下去。當發生死鎖的線程在執行系統的關鍵功能時,那麼這個死鎖可能會致使整個系統癱瘓,要想恢復系統,臨時也是惟一的規避方法是將系統重啓。而後趕快去修改致使這個死鎖的Bug。
注意:死鎖的兩個或多個線程是不消耗CPU的,有的人認爲CPU100%的使用率是線程死鎖致使的,這個說法是徹底錯誤的。死循環,而且在循環中代碼都是CPU密集型,纔有可能致使CPU的100%使用率,像socket或者數據庫等IO操做是不怎麼消耗CPU的。