浅谈 Java 多线程环境下 如何跟踪死锁 (例子)

1)源代码如下:

package ThreadDeadLockTrial;

import java.util.*;   
  
class Thread1 extends Thread {  
        @Override  
        public void run() {  
            System.out.println(“thread 1 start”);  
            synchronized (ThreadDeadTrial.lock_1) {
            System.out.println(“thread 1 locked lock1”);  
                try {  
                    Thread.sleep(5000);  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
                System.out.println(“thread 1 wait for lock2”);  
                synchronized (ThreadDeadTrial.lock_2) {  
                System.out.println(“thread 1 locked lock2”);  
                }  
            }  
        System.out.println(“thread 1 end”);  
        }  
    }  
  
class Thread2 extends Thread {
        @Override  
        public void run() {  
            System.out.println(“thread 2 start”);  
            synchronized (ThreadDeadTrial.lock_2) {  
            System.out.println(“thread 2 locked lock2”);  
                try {  
                    Thread.sleep(5000);  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
                System.out.println(“thread 2 wait for lock1”);  
                synchronized (ThreadDeadTrial.lock_1) {  
                System.out.println(“thread 2 locked lock1”);  
                }  
            }  
         System.out.println(“thread 2 end”);   
        }  
   }  
 
 
public class ThreadDeadTrial {

public static Object lock_1 = new int[1];  
public static Object lock_2 = new int[1];  
    
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread1 thread1 = new Thread1();  
        Thread2 thread2 = new Thread2(); 
        thread1.start();
        thread2.start();
        System.out.println(“Main thread exit”);
        
}

}

当运行这个程序时,program就会死循环,等在这里了。

如何check ? (jps and jstack 都事jdk自己带的)

1)  jps

10340 ThreadDeadTrial
7124
7668 Jps

2)  jstack 10340  > .\deaddump.data

 

3) open deaddump.data,就能看到下面的信息了,注意红字和蓝色字。够明显的了吧

2016-01-19 16:48:28

Full thread dump Java HotSpot(TM) Client VM (25.40-b25 mixed mode, sharing):

“DestroyJavaVM” #10 prio=5 os_prio=0 tid=0x0239c400 nid=0x25d4 waiting on condition [0x00000000]

   java.lang.Thread.State: RUNNABLE

“Thread-1” #9 prio=5 os_prio=0 tid=0x01e8f800 nid=0x3dc waiting for monitor entry [0x05fbf000]

   java.lang.Thread.State: BLOCKED (on object monitor)

at ThreadDeadLockTrial.Thread2.run(ThreadDeadTrial.java:37)


waiting to lock <0x102b85c8> (a [I)
– locked <0x102b85d8> (a [I)

“Thread-0” #8 prio=5 os_prio=0 tid=0x01e8d400 nid=0x1f3c waiting for monitor entry [0x0514f000]

   java.lang.Thread.State: BLOCKED (on object monitor)

at ThreadDeadLockTrial.Thread1.run(ThreadDeadTrial.java:17)


– waiting to lock <0x102b85d8> (a [I)
– locked <0x102b85c8> (a [I)

“Service Thread” #7 daemon prio=9 os_prio=0 tid=0x01e57000 nid=0x2250 runnable [0x00000000]

   java.lang.Thread.State: RUNNABLE

“C1 CompilerThread0” #6 daemon prio=9 os_prio=2 tid=0x01e4fc00 nid=0x17c4 waiting on condition [0x00000000]

   java.lang.Thread.State: RUNNABLE

“Attach Listener” #5 daemon prio=5 os_prio=2 tid=0x01e4ec00 nid=0x2ac0 waiting on condition [0x00000000]

   java.lang.Thread.State: RUNNABLE

“Signal Dispatcher” #4 daemon prio=9 os_prio=2 tid=0x01e3c400 nid=0x20c8 runnable [0x00000000]

   java.lang.Thread.State: RUNNABLE

“Finalizer” #3 daemon prio=8 os_prio=1 tid=0x01dd3400 nid=0x248c in Object.wait() [0x0570f000]

   java.lang.Thread.State: WAITING (on object monitor)

at java.lang.Object.wait(Native Method)

– waiting on <0x10206dd0> (a java.lang.ref.ReferenceQueue$Lock)

at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)

– locked <0x10206dd0> (a java.lang.ref.ReferenceQueue$Lock)

at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)

at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)

“Reference Handler” #2 daemon prio=10 os_prio=2 tid=0x01dd1800 nid=0x2a9c in Object.wait() [0x04fcf000]

   java.lang.Thread.State: WAITING (on object monitor)

at java.lang.Object.wait(Native Method)

– waiting on <0x102068c8> (a java.lang.ref.Reference$Lock)

at java.lang.Object.wait(Object.java:502)

at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:157)

– locked <0x102068c8> (a java.lang.ref.Reference$Lock)

“VM Thread” os_prio=2 tid=0x01dd0400 nid=0x2a34 runnable 

“VM Periodic Task Thread” os_prio=2 tid=0x01e58400 nid=0x2898 waiting on condition 

JNI global references: 6

Found one Java-level deadlock:
=============================
“Thread-1”:
  waiting to lock monitor 0x01dd5d14 (object 0x102b85c8, a [I),
  which is held by “Thread-0”
“Thread-0”:
  waiting to lock monitor 0x01dd8244 (object 0x102b85d8, a [I),
  which is held by “Thread-1”

Java stack information for the threads listed above:

===================================================

“Thread-1”:

at ThreadDeadLockTrial.Thread2.run(ThreadDeadTrial.java:37)

– waiting to lock <0x102b85c8> (a [I)

– locked <0x102b85d8> (a [I)

“Thread-0”:

at ThreadDeadLockTrial.Thread1.run(ThreadDeadTrial.java:17)

– waiting to lock <0x102b85d8> (a [I)

– locked <0x102b85c8> (a [I)

Found 1 deadlock.

    原文作者:java锁
    原文地址: https://blog.csdn.net/bigtree_3721/article/details/50543595
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞