死锁的示例
作者:
骇客与画家 | 来源:发表于
2018-01-11 00:56 被阅读0次package day12;
/*
同步锁的示例
*/
class Test implements Runnable{
private boolean flag;
Test(boolean flag){
this.flag = flag;
}
public void run(){
if (flag){
while (true){
synchronized (MyLock.locka){
System.out.println("if locka....");
synchronized (MyLock.lockb){
System.out.println("if locka....");
}
}
}
}else {
while (true){
synchronized (MyLock.lockb){
System.out.println("else lockb");
synchronized (MyLock.locka){
System.out.println("else locka");
}
}
}
}
}
}
class MyLock{
public static final Object locka = new Object();
public static final Object lockb = new Object();
}
public class DeadLockTest {
public static void main(String[] args) {
Test a = new Test(true);
Test b = new Test(false);
Thread t1 = new Thread(a);
Thread t2 = new Thread(b);
t1.start();
t2.start();
}
}
本文标题:死锁的示例
本文链接:https://www.haomeiwen.com/subject/plaanxtx.html
网友评论