Java死锁以及命令检测

Java每个对象都有一把锁,当前进程使用对象锁1,没有释放该锁,又想要去获取另一把对象锁2,而对象锁2被另外一个线程持有,没有释放,这就很容易出现死锁

1.死锁实例

public class DeadLockTest {

	private static Object object1 = new Object();
	private static Object object2 = new Object();

	public static void main(String[] args) {
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				synchronized (object1) {
					System.out.println("线程1获取object1锁...");
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					synchronized (object2) {
						System.out.println("线程1获取object2锁...");
					}
				}
			}
		}).start();
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				synchronized (object2) {
					System.out.println("线程2获取object2锁...");
					synchronized (object1) {
						System.out.println("线程2获取object1锁...");
					}
				}
			}
		}).start();
		
		System.out.println("main 线程执行完毕...");
	}
}

 分析:线程1持有object1的锁,同时又想去获取object2的锁,而此时线程2又持有object2的锁没有释放,又想去获取object1的锁,这就造成了死锁。

2.通过命令查看死锁

前提:进入jdk的bin目录下

2.1 jps

jps命令查看Java相关进程

2.2 jstack

jstack:查看分析具体的进程执行情况

运行上面的程序,执行jps命令,发现上面代码的进程号为14888

然后执行jstack -l pid(进程号)

C:\Program Files\Java\jdk1.8.0_51\bin>jstack -l 14144
2018-07-15 23:44:43
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.51-b03 mixed mode):

"DestroyJavaVM" #13 prio=5 os_prio=0 tid=0x0000000004dd3800 nid=0x3808 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

"Thread-1" #12 prio=5 os_prio=0 tid=0x000000001c1a1000 nid=0x2af4 waiting for monitor entry [0x000000001ce2f000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at com.liu.demo.test.thread.DeadLockTest$2.run(DeadLockTest.java:36)
        - waiting to lock <0x00000000d60ba308> (a java.lang.Object)
        - locked <0x00000000d60ba318> (a java.lang.Object)
        at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
        - None

"Thread-0" #11 prio=5 os_prio=0 tid=0x000000001c1a0000 nid=0x6d0 waiting for monitor entry [0x000000001cd2f000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at com.liu.demo.test.thread.DeadLockTest$1.run(DeadLockTest.java:23)
        - waiting to lock <0x00000000d60ba318> (a java.lang.Object)
        - locked <0x00000000d60ba308> (a java.lang.Object)
        at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
        - None

"Service Thread" #10 daemon prio=9 os_prio=0 tid=0x000000001c0df000 nid=0x2c0c runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

"C1 CompilerThread3" #9 daemon prio=9 os_prio=2 tid=0x000000001c0c7000 nid=0x41ac waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

"C2 CompilerThread2" #8 daemon prio=9 os_prio=2 tid=0x000000001c0bf800 nid=0x27e8 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

"C2 CompilerThread1" #7 daemon prio=9 os_prio=2 tid=0x000000001c0be000 nid=0x3728 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

"C2 CompilerThread0" #6 daemon prio=9 os_prio=2 tid=0x000000001c0b9800 nid=0x2f08 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000000001c0b6800 nid=0x377c waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000000001c06a800 nid=0x1f48 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x0000000004ecc000 nid=0x798 in Object.wait() [0x000000001bf2f000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x00000000d5f06f58> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
        - locked <0x00000000d5f06f58> (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)

   Locked ownable synchronizers:
        - None

"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x0000000004ec5000 nid=0x43ec in Object.wait() [0x000000001be2f000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x00000000d5f06998> (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 <0x00000000d5f06998> (a java.lang.ref.Reference$Lock)

   Locked ownable synchronizers:
        - None

"VM Thread" os_prio=2 tid=0x0000000019f38000 nid=0x2f88 runnable

"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000000004de9800 nid=0x1324 runnable

"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x0000000004deb000 nid=0x1e24 runnable

"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x0000000004dec800 nid=0x1bb4 runnable

"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x0000000004dee000 nid=0x36c4 runnable

"GC task thread#4 (ParallelGC)" os_prio=0 tid=0x0000000004df0800 nid=0x3838 runnable

"GC task thread#5 (ParallelGC)" os_prio=0 tid=0x0000000004df2800 nid=0x1d2c runnable

"GC task thread#6 (ParallelGC)" os_prio=0 tid=0x0000000004df5800 nid=0x4214 runnable

"GC task thread#7 (ParallelGC)" os_prio=0 tid=0x0000000004df7000 nid=0x3018 runnable

"VM Periodic Task Thread" os_prio=2 tid=0x000000001c159000 nid=0x266c waiting on condition

JNI global references: 6


Found one Java-level deadlock:
=============================
"Thread-1":
  waiting to lock monitor 0x0000000004eca3e8 (object 0x00000000d60ba308, a java.lang.Object),
  which is held by "Thread-0"
"Thread-0":
  waiting to lock monitor 0x0000000004ecb7d8 (object 0x00000000d60ba318, a java.lang.Object),
  which is held by "Thread-1"

Java stack information for the threads listed above:
===================================================
"Thread-1":
        at com.liu.demo.test.thread.DeadLockTest$2.run(DeadLockTest.java:36)
        - waiting to lock <0x00000000d60ba308> (a java.lang.Object)
        - locked <0x00000000d60ba318> (a java.lang.Object)
        at java.lang.Thread.run(Thread.java:745)
"Thread-0":
        at com.liu.demo.test.thread.DeadLockTest$1.run(DeadLockTest.java:23)
        - waiting to lock <0x00000000d60ba318> (a java.lang.Object)
        - locked <0x00000000d60ba308> (a java.lang.Object)
        at java.lang.Thread.run(Thread.java:745)

Found 1 deadlock.

从上面的结果中可以看到,两个线程都处于BLOCKED阻塞状态,并且明确指出发现了一个死锁;

posted @ 2018-07-15 23:48  HappyCowboy  阅读(685)  评论(0编辑  收藏  举报