黑马程序员技术交流社区
标题:
死锁
[打印本页]
作者:
outsider1020
时间:
2014-12-23 10:34
标题:
死锁
死锁的代码实现:
public class TestDeadLock implements Runnable {
public int flag= 1;
static Object o1=new Object() , o2 =new Object() ;
public void run() {
System .out. println("flag=" +flag);
if (flag== 1) {
synchronized (o1) {
try {
Thread .sleep( 500);
} catch (InterruptedException e) {
e .printStackTrace() ;
}
synchronized (o2) {
System .out. println("1" );
}
}
}
if (flag== 0) {
synchronized (o2) {
try {
Thread .sleep( 500);
} catch (InterruptedException e) {
e .printStackTrace() ;
}
synchronized (o1) {
System .out. println("0" );
}
}
}
}
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() ;
}
}
作者:
南柯一梦境
时间:
2014-12-23 11:17
楼主厉害啊
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2