package Thread;
/**
* 死锁范例
* @author Again
*
*/
class Demo implements Runnable{
private boolean flag;
public Demo(boolean flag){
this.flag=flag;
}
public void run(){
if(flag){
synchronized (Lock.lockA) {
System.out.println("if lockA");
synchronized (Lock.lockB) {
System.out.println("if lockB");
}
}
}else{
synchronized (Lock.lockB) {
System.out.println("else lockB");
synchronized (Lock.lockA) {
System.out.println("else lockA");
}
}
}
}
}
class Lock{
static Lock lockA=new Lock();
static Lock lockB=new Lock();
}
public class DeadLockDemo {
public static void main(String[] args){
Thread t1=new Thread(new Demo(true));
Thread t2=new Thread(new Demo(false));
t1.start();
t2.start();
}
}
多线程死锁范例
最新推荐文章于 2021-06-22 23:29:45 发布