La commande jstack peut être utilisée pour vérifier l'état d'exécution des multi-threads sous le processus Java en cours d'exécution, telles que "[root@admin ~]# jstack 43616. Dump de thread complet Java HotSpot(TM) 64-Bit Server...".
Recommandé : "tutoriel Java"
Explication détaillée de la commande Java jstack
La commande jstack en Java peut être utilisée pour afficher l'état d'exécution des multi-threads sous le processus Java en cours d'exécution. exemple de blocage à regarder :
Par exemple, le code de blocage suivant :
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对象的锁"); } } } } }
Bien sûr, le résultat en cours d'exécution ne peut voir que que les deux threads n'ont obtenu qu'un seul verrou et attendent l'autre le verrou de la partie. >
线程1得到A对象的锁 线程2得到B对象的锁Nous pouvons utiliser jps pour afficher le PID correspondant, puis utiliser jstack pour afficher l'état de son fil :
[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.Nous pouvons voir que jstack affiche l'état du fil et trouve un 🎜>
De plus, l'état du thread a les types suivants :
- Le thread RUNNABLE est en cours d'exécution ou E/S en attente
- Le thread BLOCKED attend le verrouillage du moniteur (synchronisé). mot-clé)
- Le fil TIMED_WAITING attend de se réveiller, mais le délai est défini
- Le fil WAITING attend infiniment de se réveiller
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!