高并发2Monitor

🔒是非常重要的一个概念.
在Java中有大量的锁的应用.来保证多线程环境下程序运行的结果的好坏.

实例1

	private int count = 0;
    // o ,存在堆内存中
    private Object o = new Object();
    @Test
    public void m(){
        // 在执行sync内的代码块的时候,需要获取o对象的对象锁,对象所在o这个实例中
        // 互斥锁,只有一个线程能够拿到这把锁🔒
        synchronized (o){
            count--;
            System.out.println(Thread.currentThread().getName() + "count==" + count);
        }
    }
这里的一个测试方法.使用了synchronized 关键字.来保证程序运行的结果符合预期.
synchronized 括号内锁定的是一个对象.
这么说也不对.
应该说
这里锁定的并不是对象.也不是这个对象的地址.而是这个对象的对象头.
可以说,对象头,才是🔒的精髓.

实例2

@Test
    public void m2(){
        // 这里的🔒锁的是自身
        synchronized (this){
            count--;
            System.out.println(Thread.currentThread().getName() + "count==" + count);
        }
    }
上面的那个代码太烦人了.谁没事干动不动的new一个对象,🔒起来啊.
于是乎,也可以这么写. 🔒this,也就是自身的实例.锁住了自己对象头.
在访问这个方法的时候,需要先拿到这个对象的锁,也就是自身的锁.
比如:A和B俩个线程同时访问这个方法.但是,但是使用的C的方法,而
这个C是独一份,A和B使用的是一份.那怎么办呢.
于是乎在这个方法内部,也就是在执行这一块的代码的时候,需要先拿到this🔒.
拿到🔒的先执行.

疑问

🔒是独一份的. 只有一个线程能够拿到.那么问题来了. 一个方法锁住了.
那么,另一个方法访问的时候,会不会导致阻塞呢???

解决

大家知道我们的java文件最后都会转换成为class文件,被jvm执行.
我们来看一看这个究竟做了什么

在这里插入图片描述

标红的俩行,正是这个synchronized 被解析为class后的代码片段.
而标红的俩行,则是jvm的指令;java或者说,所有代码的本质.
关于这俩条指令,参考jvm的描述,规范:
关于这两条指令的作用,我们直接参考JVM规范中描述:
monitorenter :
Each object is associated with a monitor.
 A monitor is locked if and only if it has an owner. 
 The thread that executes monitorenter attempts to gain ownership of the monitor associated with objectref, as follows:
• If the entry count of the monitor associated with objectref is zero, the thread enters the monitor and sets its entry count to one. The thread is then the owner of the monitor.
• If the thread already owns the monitor associated with objectref, it reenters the monitor, incrementing its entry count.
• If another thread already owns the monitor associated with objectref, the thread blocks until the monitor's entry count is zero, then tries again to gain ownership.

这段话的大概意思为:

每个对象有一个监视器锁(monitor)。当monitor被占用时就会处于锁定状态,线程执行monitorenter指令时尝试获取monitor的所有权,过程如下:

1、如果monitor的进入数为0,则该线程进入monitor,然后将进入数设置为1,该线程即为monitor的所有者。

2、如果线程已经占有该monitor,只是重新进入,则进入monitor的进入数加1.

3.如果其他线程已经占用了monitor,则该线程进入阻塞状态,直到monitor的进入数为0,再重新尝试获取monitor的所有权。

monitorexit: 

The thread that executes monitorexit must be the owner of the monitor associated with the instance referenced by objectref.
The thread decrements the entry count of the monitor associated with objectref. If as a result the value of the entry count is zero, the thread exits the monitor and is no longer its owner. Other threads that are blocking to enter the monitor are allowed to attempt to do so.
这段话的大概意思为:
执行monitorexit的线程必须是objectref所对应的monitor的所有者。
指令执行时,monitor的进入数减1,如果减1后进入数为0,那线程退出monitor,不再是这个monitor的所有者。其他被这个monitor阻塞的线程可以尝试去获取这个 monitor 的所有权。 
通过这两段描述,我们应该能很清楚的看出Synchronized的实现原理,Synchronized的语义底层是通过一个monitor的对象来完成,其实wait/notify等方法也依赖于monitor对象,这就是为什么只有在同步的块或者方法中才能调用wait/notify等方法,否则会抛出java.lang.IllegalMonitorStateException的异常的原因。

总结:

每个对象,都有一个监视器🔒,每次线程进入时,则监视器🔒 会 + 1.
 在此进入时,在+1. 此时,如果别的线程想要进入,则等待监视器🔒的值为0,即可.否则阻塞.
大概是这个意思.
也就是说,synchronized  是通过底层的Monitor 对象来完成方法同步的,进而实现线程安全的.

仿照

那是不是说,synchronized  可以用这段代码来实现方法同步啊.
public static volatile int i = 0;
    
    @Test
    public void test1(){
        if(i == 1){
            System.out.println("访问到了");
        }
    }
    
    @Test
    public void test2(){
        if (i == 0) {
            System.out.println("访问到哦我了");
        }
    }
使用一个volatile变量,保证这个变量的可见性.即:每次修改变量值后,在主存中的值都是最新的.
也可以理解为:每次这个volatile的值修改后,其他线程内的这个i值,作废了.不生效了.只能够在去主存中去取最新的值.
我们按照这个i的值来判断,如果是1的话,则执行第一个方法,如果是2的话,执行第二个方法.
跟Monitor 的值决定是不是阻塞,来判断方法是否被第二个线程执行的思想基本一致
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值