- //死锁
- class Test implements Runnable
- {
- private boolean flag;
- Test(boolean flag){
- this.flag=flag;
- }
- Object b1=new Object();//创建锁
- Object b2=new Object();//创建锁
- public void run(){
- if(flag)
- {
- synchronized(b1){//添加同步代码块 b1为锁
- System.out.println("if b1");
- synchronized(b2){///在同步代码块中嵌套同步代码块 锁为b2
- System.out.println("if b2");
- }
- }
- }else{
- synchronized(b2){
- System.out.println("else b2");
- synchronized(b1){
- System.out.println("else b1");
- }
- }
- }
- }
- }
- class DeadLockDemo
- {
- public static void main(String[] args)
- {
- Thread t=new Thread(new Test(true));
- Thread t1=new Thread(new Test(false));
- t.start();
- t1.start();
- }
- }
复制代码
为什么我这里产生不了死锁?? |
|