1. DeadThread.java
public class DeadThread implements Runnable{
private Object monitor_A = new Object();
private Object monitor_B = new Object();
public void method_A(){
synchronized(monitor_A) {
synchronized(monitor_B) {
System.out.println(Thread.currentThread().getName()+" invoke method A");
}
}
}
public void method_B(){
synchronized(monitor_B) {
synchronized(monitor_A) {
System.out.println(Thread.currentThread().getName()+" invoke method B");
}
}
}
public void run() {
for(int i=0;i<1 class="Apple-tab-span" i--="" span="" style="white-space: pre;"> 1>
method_A(); public class DeadThread implements Runnable{
private Object monitor_A = new Object();
private Object monitor_B = new Object();
public void method_A(){
synchronized(monitor_A) {
synchronized(monitor_B) {
System.out.println(Thread.currentThread().getName()+" invoke method A");
}
}
}
public void method_B(){
synchronized(monitor_B) {
synchronized(monitor_A) {
System.out.println(Thread.currentThread().getName()+" invoke method B");
}
}
}
public void run() {
for(int i=0;i<1 class="Apple-tab-span" i--="" span="" style="white-space: pre;"> 1>
method_B();
}
}
public static void main(String[] args) {
DeadThread t1 = new DeadThread();
Thread ta = new Thread(t1, "A");
Thread tb = new Thread(t1, "B");
ta.start();
tb.start();
}
}
2. 线程A和线程B的thread dump
"B" prio=5 tid=0x00007fefa8886800 nid=0x5903 waiting for monitor entry [0x000000011daa6000]
java.lang.Thread.State: BLOCKED (on object monitor)
at DeadThread.method_A(DeadThread.java:10)
- waiting to lock <0x00000007aab2c0b0> (a java.lang.Object)0x00000007aab2c0b0>
- locked <0x00000007aab2c0a0> (a java.lang.Object)0x00000007aab2c0a0>
at DeadThread.run(DeadThread.java:25)
at java.lang.Thread.run(Thread.java:744)
"A" prio=5 tid=0x00007fefa8885800 nid=0x5703 waiting for monitor entry [0x000000011d9a3000]
java.lang.Thread.State: BLOCKED (on object monitor)
at DeadThread.method_B(DeadThread.java:18)
- waiting to lock <0x00000007aab2c0a0> (a java.lang.Object)0x00000007aab2c0a0>
- locked <0x00000007aab2c0b0> (a java.lang.Object)0x00000007aab2c0b0>
at DeadThread.run(DeadThread.java:26)
at java.lang.Thread.run(Thread.java:744)
3. 直接报告死锁的thread dump
Found one Java-level deadlock:
=============================
"B":
waiting to lock monitor 0x00007fefa8850cb8 (object 0x00000007aab2c0b0, a java.lang.Object),
which is held by "A"
"A":
waiting to lock monitor 0x00007fefa88522b8 (object 0x00000007aab2c0a0, a java.lang.Object),
which is held by "B"
Java stack information for the threads listed above:
===================================================
"B":
at DeadThread.method_A(DeadThread.java:10)
- waiting to lock <0x00000007aab2c0b0> (a java.lang.Object)0x00000007aab2c0b0>
- locked <0x00000007aab2c0a0> (a java.lang.Object)0x00000007aab2c0a0>
at DeadThread.run(DeadThread.java:25)
at java.lang.Thread.run(Thread.java:744)
"A":
at DeadThread.method_B(DeadThread.java:18)
- waiting to lock <0x00000007aab2c0a0> (a java.lang.Object)0x00000007aab2c0a0>
- locked <0x00000007aab2c0b0> (a java.lang.Object)0x00000007aab2c0b0>
at DeadThread.run(DeadThread.java:26)
at java.lang.Thread.run(Thread.java:744)
Found 1 deadlock.
参考文献:
1. http://blog.csdn.net/jackie_xiaonan/article/details/8546541
2. http://www.cnblogs.com/tomsheep/archive/2010/06/10/1755840.html
1 条评论:
初学者程序员的java代码示例
从命令行隐藏密码
发表评论