Synchronized三种使用方式

1、代码块(锁括号内的对象)

  • Object o = new Object();
    synchronized (o){
                
    }

2、实例方法 (this)

public synchronized  void methods02(){
            System.out.println("methods02");
 }

3、静态方法(类对象)

public static synchronized  void methods02(){
            System.out.println("methods02");
 }

 区别:

1、有ab两个线程,请问先打印methods01还是methods02 (methods01)

  资源类

static class Test{
    public synchronized void methods01(){
        System.out.println("methods01");
    }
    public synchronized  void methods02(){
        System.out.println("methods02");
    }
} 

 结果说明:methods01先调用,先执行。

2、methods01方法中加入暂停3秒钟,请问先打印methods01还是methods02 (methods02)

static class Test{
    public synchronized void methods01(){
        try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }
        System.out.println("methods01");
    }
    public synchronized  void methods02(){
        System.out.println("methods02");
    }
}

 结果说明:两个方法调用同一个锁,即锁的this,被methods01抢夺后,methods02进入等待

3、添加一个普通的hello方法,请问先打印methods01还是hello (hello)

static class Test{
    public  synchronized void methods01(){
        try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }
        System.out.println("methods01");
    }
    public  synchronized  void methods02(){
        System.out.println("methods02");
    }
    public void hello(){
        System.out.println("hello");
    }
}

 hello没加锁不用等待methods01执行完

4、有两个实例对象,请问先打印methods01还是methods02 (methods02)

两个方法methods01锁住test实例对象,methods02锁住test02对象,methods02没有sleep先打印 

5、有两个静态同步方法,有1个实例,请问先打印methods01还是methods02 (methods01)

static class Test{
    public static   synchronized void methods01(){
        try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }
        System.out.println("methods01");
    }
    public static  synchronized  void methods02(){
        System.out.println("methods02");
    }
    public void hello(){
        System.out.println("hello");
    }
}        

static锁住类对象,只有一个,需要等待methods01执行完。

6、有两个静态同步方法,有2部手机,请问先打印methods01还是methods02(methods01) 原因和5一样
7、有1个静态同步方法,有1个普通同步方法,有1个实例,请问先打印methods01还是methods02

static class Test{
    public static   synchronized void methods01(){
        try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }
        System.out.println("methods01");
    }
    public  synchronized  void methods02(){
        System.out.println("methods02");
    }
    public void hello(){
        System.out.println("hello");
    }
}

methods01拿类锁,methods02拿对象锁,methods02不用等待 

8、有1个静态同步方法,有1个普通同步方法,有2个实例,请问先打印methods01还是methods02

原因同7 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值