/*关于死锁
意思是,我持有一个锁,你持有一个锁,我要到你里面去运行,我要拿你的锁,你要到我里面去
运行,要拿我的锁,我不放我的锁,你不放你的锁,就会出现死锁,最终会让程序停止掉
*/
class Lock implements Runnable
{
private boolean flag;
Lock(boolean flag)
{
this.flag=flag;
}
public void run()
{
if(flag)
{
synchronized(LockDemo.locka)
{
System.out.println("if locka");
synchronized(LockDemo.lockb)
{
System.out.println("if lockb");
}
}
}
else
{
synchronized(LockDemo.lockb)
{
System.out.println("else lockb");
synchronized(LockDemo.locka)
{
System.out.println("else locka");
}
}
}
}
}
class LockDemo
{
static Object locka=new Object();
static Object lockb=new Object();
}
class DeadLock
{
public static void main(String[] args)
{
Thread t1=new Thread(new Lock(true));
Thread t2=new Thread(new Lock(false));
t1.start();
t2.start();
}
}