[Interview Question] Simple understanding of the upgrade of the Synchronized four lock states

Introduction

Locking allows only one thread to access a piece of code at the same time. While increasing security, it sacrifices the execution performance of the program. Therefore, in order to reduce the performance cost of acquiring and releasing locks to a certain extent, in After jdk6, "biased lock" and "lightweight lock" were introduced, so there are a total of 4 lock states, from low to high level: no lock state, biased lock state, lightweight lock state, heavyweight lock status. These states will gradually escalate with competition.


Lock upgrade flowchart

Insert picture description here

Insert picture description here


Reference: https://www.jianshu.com/p/d61f294ac1a6

Guess you like

Origin blog.csdn.net/qq_42380734/article/details/108502026