测试java死锁,锁定对象为什么要用static修饰呢?

写了小程度测试java死锁。
我的疑问是:被锁定对象为什么要用static修饰呢?不用static就锁不住,不能出现死锁了。
未用static修饰的代码如下 :

public class TestDeadLock implements Runnable{
    public int flag = 0;
     Object o1 = new Object();    Object o2 = new Object();
    public void run(){
        System.out.println("flag=" + flag);
        if(flag==1){
            synchronized(o1){
                System.out.println("1"+o2.hashCode());  

                try{
                    Thread.sleep(1500);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }
                synchronized(o2){
                    //System.out.println("1"+o2.hashCode());    
                }
            }


        }

        if(flag==0){
            synchronized(o2){
                System.out.println("1"+o2.hashCode());  

                try{
                    Thread.sleep(1500);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }
                synchronized(o1){
                //System.out.println("0"+o2.hashCode());
                }
            }

        }
    }
    public static void main(String[] args){
        TestDeadLock td1 = new TestDeadLock();
        TestDeadLock td2 = new TestDeadLock();
        td1.flag = 1;
        td2.flag = 0;
        Thread t1 = new Thread(td1);
        Thread t2 = new Thread(td2);
        t1.start();
        t2.start();

    }
}

个人理解:不加static,TestDeadLock类在main方法中被new 了二次,td1和td2各自分别产生了o1,o2。run()方法去锁定时,也只能锁定td1和td2中各自的o1,o2对象
。加了 static后,TestDeadLock类无论new多少次,都只产生一个o1,一个o2对象。run()方法去锁定时,也就是锁定的唯一的,o1,o2.
测试方法:打印o1,o2的 hashcode();进行判断产生了几个o1,o2对象。

用static修饰的代码如下 :

public class TestDeadLock implements Runnable{
    public int flag = 0;
     static Object o1 = new Object();    static Object o2 = new Object();
    public void run(){
        System.out.println("flag=" + flag);
        if(flag==1){
            synchronized(o1){
                System.out.println("1"+o2.hashCode());  

                try{
                    Thread.sleep(1500);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }
                synchronized(o2){
                    //System.out.println("1"+o2.hashCode());    
                }
            }


        }

        if(flag==0){
            synchronized(o2){
                System.out.println("1"+o2.hashCode());  

                try{
                    Thread.sleep(1500);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }
                synchronized(o1){
                //System.out.println("0"+o2.hashCode());
                }
            }

        }
    }
    public static void main(String[] args){
        TestDeadLock td1 = new TestDeadLock();
        TestDeadLock td2 = new TestDeadLock();
        td1.flag = 1;
        td2.flag = 0;
        Thread t1 = new Thread(td1);
        Thread t2 = new Thread(td2);
        t1.start();
        t2.start();

    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值