package com.jikexueyuan.myviewpager;
/**
* Created by lcj on 16/10/11.
*/
public class DeadLockDemo {
public static void main(String arg []){
new DeadLock();
}
static class Tom{
synchronized public void lend(Lucy l){
System.out.println("lend 获得筷子");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
l.eat();
}
synchronized public void eat() {
System.out.println("eat 开始吃饭");
}
}
static class Lucy{
synchronized public void lend(Tom t){
System.out.println("Tom 获得筷子");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
t.eat();
}
synchronized public void eat() {
System.out.println("Tom 开始吃饭");
}
}
static class DeadLock extends Thread{
Lucy l = new Lucy();
Tom t = new Tom();
public DeadLock(){
this.start();
l.lend(t);
}
@Override
public void run() {
super.run();
t.lend(l);
}
}
}
Java 死锁demo
最新推荐文章于 2022-05-16 21:42:22 发布