package com.xwolf.java.thread.ch2; /**
* Created by Administrator on 2016/1/4 0004.
*/
public class DeadLock {
private static String a ="A";
private static String b ="B"; public static void main(String[] args) {
new DeadLock().deadLock();
} private void deadLock(){
Thread thread1 = new Thread(new Runnable() { public void run() { synchronized (a) {
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} synchronized (b) {
System.out.println("Thread-0001");
}
} }
}); Thread thread2 = new Thread(new Runnable() { public void run() { synchronized (b) {
synchronized (a) {
System.out.println("Thread-0002");
}
} }
}); thread1.start();
thread2.start();
}
}
\D:\Program Files\Java\jdk1.7.0_79\bin>jps
11212 Launcher
7332 Jps
2744 RemoteMavenServer
10108 NailgunRunner
7760 AppMain
7152
D:\Program Files\Java\jdk1.7.0_79\bin>jstack -l 7760
2016-01-04 16:36:17
Full thread dump Java HotSpot(TM) 64-Bit Server VM (24.79-b02 mixed mode):
"DestroyJavaVM" prio=6 tid=0x0000000002745000 nid=0x2aa8 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"Thread-1" prio=6 tid=0x000000000ef63800 nid=0x13ec waiting for monitor entry [0x000000000fa1f000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.xwolf.java.thread.ch2.DeadLock$2.run(DeadLock.java:44)
- waiting to lock <0x00000007c0b57ee8> (a java.lang.String)
- locked <0x00000007c0b57f18> (a java.lang.String)
at java.lang.Thread.run(Thread.java:745)
Locked ownable synchronizers:
- None
"Thread-0" prio=6 tid=0x000000000ef62800 nid=0x1c58 waiting for monitor entry [0x000000000f91f000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.xwolf.java.thread.ch2.DeadLock$1.run(DeadLock.java:28)
- waiting to lock <0x00000007c0b57f18> (a java.lang.String)
- locked <0x00000007c0b57ee8> (a java.lang.String)
at java.lang.Thread.run(Thread.java:745)
Locked ownable synchronizers:
- None
"Monitor Ctrl-Break" daemon prio=6 tid=0x000000000ef72000 nid=0x18d4 runnable [0x000000000f81e000]
java.lang.Thread.State: RUNNABLE
at java.net.DualStackPlainSocketImpl.accept0(Native Method)
at java.net.DualStackPlainSocketImpl.socketAccept(DualStackPlainSocketImpl.java:131)
at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:398)
at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:199)
- locked <0x00000007c0d3a1c8> (a java.net.SocksSocketImpl)
at java.net.ServerSocket.implAccept(ServerSocket.java:530)
at java.net.ServerSocket.accept(ServerSocket.java:498)
at com.intellij.rt.execution.application.AppMain$1.run(AppMain.java:90)
at java.lang.Thread.run(Thread.java:745)
Locked ownable synchronizers:
- None
"Service Thread" daemon prio=6 tid=0x000000000d0e5800 nid=0x12d8 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"C2 CompilerThread1" daemon prio=10 tid=0x000000000d0d6000 nid=0x2314 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"C2 CompilerThread0" daemon prio=10 tid=0x000000000d0d4800 nid=0x728 waiting on
condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"Attach Listener" daemon prio=10 tid=0x000000000d0d4000 nid=0x1b0 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"Signal Dispatcher" daemon prio=10 tid=0x000000000d0d1000 nid=0x204c runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"Finalizer" daemon prio=8 tid=0x000000000282c800 nid=0x246c in Object.wait() [0x000000000e42f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000007c0684858> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)
- locked <0x00000007c0684858> (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)
Locked ownable synchronizers:
- None
"Reference Handler" daemon prio=10 tid=0x0000000002824800 nid=0x70 in Object.wait() [0x000000000e32f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000007c0684470> (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 <0x00000007c0684470> (a java.lang.ref.Reference$Lock)
Locked ownable synchronizers:
- None
"VM Thread" prio=10 tid=0x000000000d093000 nid=0xe4c runnable
"GC task thread#0 (ParallelGC)" prio=6 tid=0x000000000275b800 nid=0x120 runnable
"GC task thread#1 (ParallelGC)" prio=6 tid=0x000000000275d800 nid=0x510 runnable
"GC task thread#2 (ParallelGC)" prio=6 tid=0x000000000275f000 nid=0x2564 runnable
"GC task thread#3 (ParallelGC)" prio=6 tid=0x0000000002761800 nid=0x244c runnable
"VM Periodic Task Thread" prio=10 tid=0x000000000d11a000 nid=0x14e8 waiting on condition
JNI global references: 153
Found one Java-level deadlock:
=============================
"Thread-1":
waiting to lock monitor 0x000000000282afa8 (object 0x00000007c0b57ee8, a java.lang.String),
which is held by "Thread-0"
"Thread-0":
waiting to lock monitor 0x000000000282c238 (object 0x00000007c0b57f18, a java.lang.String),
which is held by "Thread-1"
Java stack information for the threads listed above:
===================================================
"Thread-1":
at com.xwolf.java.thread.ch2.DeadLock$2.run(DeadLock.java:44)
- waiting to lock <0x00000007c0b57ee8> (a java.lang.String)
- locked <0x00000007c0b57f18> (a java.lang.String)
at java.lang.Thread.run(Thread.java:745)
"Thread-0":
at com.xwolf.java.thread.ch2.DeadLock$1.run(DeadLock.java:28)
- waiting to lock <0x00000007c0b57f18> (a java.lang.String)
- locked <0x00000007c0b57ee8> (a java.lang.String)
at java.lang.Thread.run(Thread.java:745)
Found 1 deadlock.
可以看到程序中的死锁问题,已经线程的一系列信息。