/**
- 死锁
- 产生死锁的原因
- 多个线程由于争夺资源而产生的资源阻塞状态
- 同步的前提
- 2个以上的线程 包含2
- 多个线程使用同一个锁
- @author WangShuang
- */
public class Demo {
public static void main(String[] args) {
Test t = new Test(true);
Thread rr=new Thread(t);
rr.start();
Test t1 = new Test(false);
Thread rr1=new Thread(t1);
rr1.start();
}
}
class MyLock{
public static Object lockA = new Object();
public static Object lockB = new Object();
}
class Test implements Runnable{
private boolean flag;
public Test( boolean flag){br/>this.flag=flag;
}
@Override
public void run() {
if(flag){
synchronized (MyLock.lockA) {
System.out.println("if lockA");
synchronized (MyLock.lockB) {
System.out.println("if lockB");
}
}
}else{
synchronized (MyLock.lockB) {
System.out.println("else lockB");
synchronized (MyLock.lockA) {
System.out.println("else lockA");
}
}
}
}
}
原文地址:http://blog.51cto.com/13579086/2065319
时间: 2024-11-09 00:29:58