package com.guoyun.ThreadTest;
/**
* ClassName:.
* Function: ADD FUNCTION
* Reason: ADD REASON
*
* @author
* @Date
* @since Ver 1.1
*/
public class DeadLockDemo {
public static void main(String[] args) {
new DeadLock("张三").start();
new DeadLock("李四").start();
}
}
class DeadLock extends Thread{
public DeadLock(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("李四拿到了遥控器和电池,可以开空调了");
}
}
}
}
}
原文地址:https://www.cnblogs.com/aojie/p/12311939.html