求解答一个Java线程死锁模拟程序失败的原因
代码如下:
public class TestDeadLock implements Runnable {
public int flag = 1;
static Object o1 = new Object();
static Object o2 = new Object();
public void run() {
System.out.println("flag = " + flag);
if(flag == 1) {
synchronized(o1) {
try {
Thread.sleep(100);
} catch(InterruptedException e) {
System.out.println("Thread.sleep()出错了。");
e.printStackTrace();
}
}
synchronized(o2) {
System.out.println("1111111");
}
}
if(flag == 0) {
synchronized(o2) {
try {
Thread.sleep(1000);
} catch(InterruptedException e) {
System.out.println("Thread.sleep()出错了。");
e.printStackTrace();
}
}
synchronized(o1) {
System.out.println("0000000");
}
}
}
public static void main(String args[]) {
TestDeadLock td1 = new TestDeadLock();
TestDeadLock td2 = new TestDeadLock();
td1.flag = 1;
td2.flag = 0;
Thread t1 = new Thread(td1);
Thread t2 = new Thread(td2);
t1.start();
t2.start();
}
}
实际输出结果为:
flag = 1
flag = 0
0000000
1111111
预期输出结果为:
flag = 1
flag = 0
求解释!
[解决办法]
你想模拟死锁,那么就要实现如下的场景:
线程t1:锁定o1,并等待锁定o2
线程t2:锁定o2,并等待锁定o1
意思是每个线程都想要同时获取两个对象的锁,而你的代码并不是“同时”获取两个锁,而是获取了一个后释放再获取另一个
改成这样就行了:
public void run() {
System.out.println("flag = " + flag);
if(flag == 1) {
synchronized(o1) {
try {
Thread.sleep(100);
} catch(InterruptedException e) {
System.out.println("Thread.sleep()出错了。");
e.printStackTrace();
}
synchronized(o2) {
System.out.println("1111111");
}
}
}
if(flag == 0) {
synchronized(o2) {
try {
Thread.sleep(1000);
} catch(InterruptedException e) {
System.out.println("Thread.sleep()出错了。");
e.printStackTrace();
}
synchronized(o1) {
System.out.println("0000000");
}
}
}
}
public class TestDeadLock implements Runnable {
public int flag = 1;
static Object o1 = new Object();
static Object o2 = new Object();
public void run() {
System.out.println("flag = " + flag);
if (flag == 1) {
synchronized (o1) {
//t1锁住了o1
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println("Thread.sleep()出错了。");
e.printStackTrace();
}
//t1尝试锁o2 ,可o2 已经被t2 给锁住了还没释放,一直等待
synchronized (o2) {
System.out.println("1111111");
}
}
//到了这里才会释放o1的锁
}
if (flag == 0) {
synchronized (o2) {
//t2锁住了o2
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Thread.sleep()出错了。");
e.printStackTrace();
}
//t2尝试锁o1,可t1锁住了o1 而且要等待t2释放o2后才能释放o1,就发生死锁了
synchronized (o1) {
System.out.println("0000000");
}
}
}
}
public static void main(String args[]) {
TestDeadLock td1 = new TestDeadLock ();
TestDeadLock td2 = new TestDeadLock ();
td1.flag = 1;
td2.flag = 0;
Thread t1 = new Thread(td1);
Thread t2 = new Thread(td2);
t1.start();
t2.start();
}
}