Operation Enter monitor for object 进入对象的监视器 Format
monitorenter Forms monitorenter = 194 (0xc2)
Operand Stack ..., objectref →
...
Description The objectref must be of type reference. 监视的对象必须是引用类型 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: 每一个对象都有一个监视器,如果一个monitor 是拥有者那么它就获得了锁,线程获得monitorenter 的使用权遵循下边的过程: 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. 如果monitor关联对象的进入次数是0,当前线程进入monitor并且设置进入次数是1,那么接下来这个线程就是这个monitor的拥有者。 If the thread already owns the monitor associated with objectref, it reenters the monitor, incrementing its entry count. 如果一个线程已经是关联对象的monitor的拥有者,那么线程再次进入monitor,会使得进入次数加1 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 的拥有者,那么当前线程会一直阻塞到进入次数为0,才能再次尝试获取monitor 的使用权。
monitorexit Operation Exit monitor for object 为了退出对象的monitor Format
monitorexit Forms monitorexit = 195 (0xc3)
Operand Stack ..., objectref →
...
Description The objectref must be of type reference. 关联的对象必须是引用类型的。 The thread that executes monitorexit must be the owner of the monitor associated with the instance referenced by objectref. 当前执行monitorexit 的线程必须是关联对象实例对象的引用上的monitor 的拥有者。 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. 当前线程减1个进入次数,正对与关联对象上的monitor 的进入此时,如果减一之后变成0,那么当前线程退出monitor , 不再是拥有者,其他阻塞的线程此时可以被允许尝试获取拥有权。