您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Java中jstack 命令的案例,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
jstack命令可以用來查看運行的Java進程下,多線程的運行情況,語句如“[root@admin ~]# jstack 43616 Full thread dump Java HotSpot(TM) 64-Bit Server...”
Java jstack 命令詳解
Java 中 jstack 命令可以用來查看運行的 Java 進程下,多線程的運行情況,我們用一個死鎖的例子來看:
比如下面這段死鎖的代碼:
public class DeadLock { private static Object objA = new Object(); private static Object objB = new Object(); public static void main(String[] args) { Thread thread1 = new Thread(new Thread1()); Thread thread2 = new Thread(new Thread2()); thread1.start(); thread2.start(); } private static class Thread1 implements Runnable{ @Override public void run() { synchronized (objA) { System.out.println("線程1得到A對象的鎖"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (objB) { System.out.println("線程1得到B對象的鎖"); } } } } private static class Thread2 implements Runnable{ @Override public void run() { synchronized (objB) { System.out.println("線程2得到B對象的鎖"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (objA) { System.out.println("線程2得到A對象的鎖"); } } } } }
當然,運行結果只能看到兩個線程只拿到了一個鎖,在一直等待對方的鎖。
線程1得到A對象的鎖 線程2得到B對象的鎖
我們可以使用 jps 來查看對應的 PID ,然后使用 jstack 來查看其線程情況:
[root@admin ~]# jps 42576 Jps 43616 DeadLock
[root@admin ~]# jstack 43616 Full thread dump Java HotSpot(TM) 64-Bit Server VM (24.45-b08 mixed mode): "DestroyJavaVM" prio=6 tid=0x00000000047c1000 nid=0x9878 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Thread-1" prio=6 tid=0x0000000010aa3000 nid=0xafa0 waiting for monitor entry [0x000000001105f000] java.lang.Thread.State: BLOCKED (on object monitor) at com.zaimeibian.Test$Thread2.run(Test.java:46) - waiting to lock <0x00000007c099cc20> (a java.lang.Object) - locked <0x00000007c099cc30> (a java.lang.Object) at java.lang.Thread.run(Thread.java:744) "Thread-0" prio=6 tid=0x0000000010aa2800 nid=0xae74 waiting for monitor entry [0x0000000010f5f000] java.lang.Thread.State: BLOCKED (on object monitor) at com.zaimeibian.Test$Thread1.run(Test.java:27) - waiting to lock <0x00000007c099cc30> (a java.lang.Object) - locked <0x00000007c099cc20> (a java.lang.Object) at java.lang.Thread.run(Thread.java:744) "Service Thread" daemon prio=6 tid=0x000000000f10a000 nid=0x9a8c runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread1" daemon prio=10 tid=0x000000000f109800 nid=0xaf28 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread0" daemon prio=10 tid=0x000000000f105800 nid=0x85dc waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Attach Listener" daemon prio=10 tid=0x000000000f104800 nid=0xac04 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Signal Dispatcher" daemon prio=10 tid=0x000000000f102000 nid=0xa678 runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Finalizer" daemon prio=8 tid=0x000000000f0bd000 nid=0xaed8 in Object.wait() [0x000000001045f000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x00000007c0905568> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135) - locked <0x00000007c0905568> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151) at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:189) "Reference Handler" daemon prio=10 tid=0x000000000f0b2000 nid=0xaedc in Object.wait() [0x000000001035f000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x00000007c09050f0> (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 <0x00000007c09050f0> (a java.lang.ref.Reference$Lock) "VM Thread" prio=10 tid=0x000000000f0b0000 nid=0xaef0 runnable "GC task thread#0 (ParallelGC)" prio=6 tid=0x00000000047d6000 nid=0xacb0 runnable "GC task thread#1 (ParallelGC)" prio=6 tid=0x00000000047d8000 nid=0xaee0 runnable "GC task thread#2 (ParallelGC)" prio=6 tid=0x00000000047d9800 nid=0xaed4 runnable "GC task thread#3 (ParallelGC)" prio=6 tid=0x00000000047db000 nid=0xac54 runnable "VM Periodic Task Thread" prio=10 tid=0x000000000f132000 nid=0xaff0 waiting on condition JNI global references: 105 Found one Java-level deadlock: ============================= "Thread-1": waiting to lock monitor 0x000000000f0ba488 (object 0x00000007c099cc20, a java.lang.Object), which is held by "Thread-0" "Thread-0": waiting to lock monitor 0x000000000f0bcf28 (object 0x00000007c099cc30, a java.lang.Object), which is held by "Thread-1" Java stack information for the threads listed above: =================================================== "Thread-1": at com.zaimeibian.Test$Thread2.run(Test.java:46) - waiting to lock <0x00000007c099cc20> (a java.lang.Object) - locked <0x00000007c099cc30> (a java.lang.Object) at java.lang.Thread.run(Thread.java:744) "Thread-0": at com.zaimeibian.Test$Thread1.run(Test.java:27) - waiting to lock <0x00000007c099cc30> (a java.lang.Object) - locked <0x00000007c099cc20> (a java.lang.Object) at java.lang.Thread.run(Thread.java:744) Found 1 deadlock.
我們可以看到 jstack 打印出了線程的狀態,而且發現一個死鎖。
另外,線程狀態有以下幾種:
- RUNNABLE 線程運行中或 I/O 等待
- BLOCKED 線程在等待 monitor 鎖( synchronized 關鍵字)
- TIMED_WAITING 線程在等待喚醒,但設置了時限
- WAITING 線程在無限等待喚醒
關于Java中jstack 命令的案例就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。