您的位置:首页 > 其它

死锁

2015-10-16 16:50 288 查看
class Test2 implements Runnable {
private boolean flag;

Test2(boolean flag) {
this.flag = flag;
}

public void run() {
if (flag) {
while (true) {
synchronized (MyLock.locka) {
System.out.println(Thread.currentThread().getName()
+ "...if locka ");
synchronized (MyLock.lockb) {
System.out.println(Thread.currentThread().getName()
+ "..if lockb");
}
}
}
} else {
while (true) {
synchronized (MyLock.lockb) {
System.out.println(Thread.currentThread().getName()
+ "..else lockb");
synchronized (MyLock.locka) {
System.out.println(Thread.currentThread().getName()
+ ".....else locka");
}
}
}
}
}
}

class MyLock {
static Object locka = new Object();
static Object lockb = new Object();
}

public class DeadLockTest {
public static void main(String[] args) {
Thread t1 = new Thread(new Test2(true));
Thread t2 = new Thread(new Test2(false));
t1.start();
t2.start();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: