Java multithreading series--upgrade of synchronized lock

Original website: Java multithreading series--upgrade of synchronized lock_IT sharp knife unsheathed blog-CSDN blog

Introduction

        This article introduces the upgrade process of Java's synchronized lock.

        The upgrade of the lock is one-way: it can only be upgraded from low to high, and there will be no downgrade of the lock.

Lock upgrade process

  1. JVM has a biased lock startup delay (default is 4 seconds)
  2. If the startup delay of the biased lock is not considered, when only one thread holds the lock, the state of the lock object is the biased lock state
  3. When multiple threads hold locks, if there is no competition for alternate locks or the number of spins of competing threads is less than a certain threshold, the biased lock is upgraded to a lightweight lock .
  4. When the number of spins exceeds a certain threshold, the lightweight lock is upgraded to a heavyweight lock.

type of lock

Bias lock

Optimistic lock: Operate competing locks through CAS.

lightweight lock

Optimistic lock: Operate competing locks through CAS.

heavyweight lock

Heavyweight locks are pessimistic locks, implemented through monitors, and each lock object has and only one monitor object of its own.

Guess you like

Origin blog.csdn.net/feiying0canglang/article/details/127046891