本帖最后由 daoyua 于 2014-1-10 16:28 编辑
class Demo implements Runnable
{
int i;
Demo(int i){this.i=i;System.out.println(this.i);}
Object c=new Object();
Object d=new Object();
public void run(){
if(i==1)
{
synchronized(c)
{
System.out.println("IFA");//try{Thread.sleep(10);}catch(Exception e){}
synchronized(d)
{
System.out.println("IFB");
}
}
}
else
{
synchronized(d)
{
System.out.println("ELSEA");
synchronized(c)
{
System.out.println("ELSEB");
}
}
}
}
}
class Suo
{
static Object a=new Object();
static Object b=new Object();
}
class Test
{
public static void main(String args[])
{
Thread t1=new Thread(new Demo(1));
Thread t2=new Thread(new Demo(2));
t1.start();
t2.start();
}
}
输出结果全部打印了,为什么不是死锁
如果把同步代码块对象换成Suo.a和Suo.b就是死锁了,不懂原因 |