zoukankan      html  css  js  c++  java
  • 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的所有权,过程如下: 

    如果monitor的进入数为0,则该线程进入monitor,然后将进入数设置为1,该线程即为monitor的所有者。
    如果线程已经占有该monitor,只是重新进入,则进入monitor的进入数加1.
    如果其他线程已经占用了monitor,则该线程进入阻塞状态,直到monitor的进入数为0,再重新尝试获取monitor的所有权。

    Synchronize 可重入锁,即如果当前线程以获得锁对象,可再次获取该锁对象即:
    该锁对象的监视器锁 monitor 具有可重入性,每进入一次,进入次数 +1
    从 synchronized 使用的语法上,如果修饰代码块,synchronize (object ) {} object 即为锁对象
    如果修饰方法,普通方法可认为是 this 锁,即当前对象锁;静态方法可认为是 类锁

     

    reference:

    https://blog.csdn.net/mingyundezuoan/article/details/79264644

  • 相关阅读:
    1012 最大公约数和最小公倍数问题 2001年NOIP全国联赛普及组
    数论笔记
    Codevs 1200 同余方程 2012年NOIP全国联赛提高组
    Codevs 1213 解的个数(exgcd)
    Qbxt 模拟赛&&day-8
    NOIP 模拟赛 那些年,我们学过的文化课 --致已退役的fqk神犇.
    OI路上 day -9
    P3178 [HAOI2015]树上操作
    P3979 遥远的国度
    P4092 [HEOI2016/TJOI2016]树
  • 原文地址:https://www.cnblogs.com/lnas01/p/10365454.html
Copyright © 2011-2022 走看看