标签:article 用户态 cut 根据 这一 record 阻塞 not 否则
初始是无锁状态。
在运行期间MarkWord里存储的数据会随着锁状态的变化而变化
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:
JVM规范对于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指令的线程必须是此Monitor对象的拥有者(否则会抛java.lang.IllegalMonitorStateException异常),线程减少Monitor对象的锁计数,如果锁计数为0了,则线程不在是Monitor对象的拥有者,其他被这个Monitor对象阻塞的线程可以尝试获取Monitor。
如果调用wait(),同上,但是会进入_WaitSet队列,等待被唤醒。(看到没:wait状态的线程在唤醒之后,还得需要获取锁④,然后执行完毕)
偏向锁相对于轻量级锁减少了CAS操作的次数,轻量级锁相对于重量级锁减少了系统调用。
public class Test {
synchronized void test() {
synchronized (this.getClass()) {}
}
}
标签:article 用户态 cut 根据 这一 record 阻塞 not 否则
原文地址:https://www.cnblogs.com/jinshuai86/p/9291033.html