黑马程序员技术交流社区
标题:
关于多线程产生死锁的问题
[打印本页]
作者:
More
时间:
2014-7-13 06:37
标题:
关于多线程产生死锁的问题
//死锁
class Test implements Runnable
{
private boolean flag;
Test(boolean flag){
this.flag=flag;
}
Object b1=new Object();//创建锁
Object b2=new Object();//创建锁
public void run(){
if(flag)
{
synchronized(b1){//添加同步代码块 b1为锁
System.out.println("if b1");
synchronized(b2){///在同步代码块中嵌套同步代码块 锁为b2
System.out.println("if b2");
}
}
}else{
synchronized(b2){
System.out.println("else b2");
synchronized(b1){
System.out.println("else b1");
}
}
}
}
}
class DeadLockDemo
{
public static void main(String[] args)
{
Thread t=new Thread(new Test(true));
Thread t1=new Thread(new Test(false));
t.start();
t1.start();
}
}
复制代码
为什么我这里产生不了死锁??
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2