日期:2014-05-18 浏览次数:20781 次
package morethread;
class MyLock
{
static Object obja = new Object();
static Object objb = new Object();
}
class Demo implements Runnable
{
boolean flag;
Demo(boolean flag)
{
this.flag = flag;
}
public void run()
{
if(flag)
synchronized(MyLock.obja)
{
System.out.println(Thread.currentThread().getName()+"obja");
synchronized(MyLock.objb)
{
System.out.println(Thread.currentThread().getName()+"objb");
}
}
else
synchronized(MyLock.objb)
{
System.out.println(Thread.currentThread().getName()+"objb");
synchronized(MyLock.obja)
{
System.out.println(Thread.currentThread().getName()+"obja");
}
}
}
}
public class DeadLockDemo
{
public static void main(String[] args)
{
Demo d1 = new Demo(true);
Demo d2 = new Demo(false);
Thread t1 = new Thread(d1);
Thread t2 = new Thread(d2);
t1.start();
t2.start();
}
}