package com.sqf.thread;
public class DealLock extends Thread{
public DealLock(String name){
super(name);
}
@Override
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 static void main(String[] args) {
DealLock thread1 = new DealLock("张三");
DealLock thread2 = new DealLock("李四");
thread1.start();
thread2.start();
}
}
