class DLock extends Thread
{
public DLock(String name)
{
super(name);
}
public void run()
{
if("张三".equals(Thread.currentThread().getName()))
{
synchronized ("遥控器")
{
System.out.println("张三拿到了遥控器,准备去拿电池");
synchronized ("电池")
{
System.out.println("合体..");
}
}
}
else if("李四".equals(Thread.currentThread().getName()))
{
synchronized ("电池")
{
System.out.println("李四拿到了电池,准备去拿遥控器");
synchronized ("遥控器")
{
System.out.println("合体...");
}
}
}
}
}
public class DeadLock
{
public static void main(String[] args)
{
DLock d1 = new DLock("张三");
DLock d2 = new DLock("李四");
d1.start();
d2.start();
}
}
张三拿到了遥控器,准备去拿电池
李四拿到了电池,准备去拿遥控器
class DLock extends Thread
{
public DLock(String name)
{
super(name);
}
public void run()
{
if("张三".equals(Thread.currentThread().getName()))
{
synchronized ("遥控器")
{
System.out.println("张三拿到了遥控器,准备去拿电池");
synchronized ("电池")
{
System.out.println("合体..");
}
}
}
else if("李四".equals(Thread.currentThread().getName()))
{
synchronized ("电池")
{
System.out.println("李四拿到了电池,准备去拿遥控器");
synchronized ("遥控器")
{
System.out.println("合体...");
}
}
}
}
}
public class DeadLock
{
public static void main(String[] args)
{
DLock d1 = new DLock("张三");
DLock d2 = new DLock("李四");
d1.start();
d2.start();
}
}
张三拿到了遥控器,准备去拿电池
李四拿到了电池,准备去拿遥控器