Various locks derived from the Java multi-threaded synchronized keyword

foreword

Keywords in Java  synchronizedcan be used as thread-safe synchronization locks in a multithreaded environment. This article does not discuss  synchronized the specific use, but studies the synchronizedunderlying lock mechanism and the advantages and disadvantages of these locks.

1. Synchronized mechanism

synchronizedThe keyword is a commonly used synchronization function in JAVA, which provides a simple and easy-to-use lock function. synchronizedThere are three usages, namely:

  • Used in ordinary methods, it can lock the current object.
  • Used on static methods to lock classes
  • Used on code blocks, synchronizedthe object in () is locked

Before JDK6, synchronizedthe heavyweight locking system was used, and synchronizedthe lock inflation mechanism was added later, which significantly improved the synchronizedefficiency of keywords.

Based on the synchronizedkeywords, let's understand the next types of locks and explain synchronizedthe lock expansion mechanism.

synchronizedLocks are unfair locks. And a synchronizedlocked object or class is a lock.

In addition, all locks are stored in the Java object header. The Mark Word in the Java object header stores the object's HashCode, generation age and lock flag bit by default. That is to say, Mark Word records the status of the lock

2. Lock Expansion Mechanism and Several Types of Locks

Lock bloat is irreversible

2.1 Bias lock

synchronizedIt is enabled by default after JDK1.6 偏向锁, synchronizedinitially偏向锁

Performance: After a thread acquires the lock successfully, the thread ID will be recorded in the object header, and the thread acquires and releases the lock without any cost in the future. (because the lock is already bound to the thread and won't change until it is bloated), if other threads try to acquire the lock, 偏向锁it will bloat as 轻量锁.

Advantage: no cost to acquire and exit locks when only one thread is using the lock

Disadvantage: Intense lock competition will quickly escalate 轻量锁, so maintaining 偏向锁the process is a waste of computer resources. (However, because it 偏向锁is very lightweight, there are not many wasted resources)

Summary: When only one thread uses synchronizedthe lock, the lock used is 偏向锁. If lock contention is intense, it can be disabled by configuring the JDK 偏向锁.

2.2 Lightweight lock

If a lock is used by more than one thread, it 偏向锁expands to轻量锁

Performance: When the thread is acquired 轻量锁, it will directly CASmodify the record of the lock in the object header. If the modification fails, it means that there is competition among multiple threads at this time, and 轻量锁it will expand to 重量锁.

Advantages: When there is no competition for the use of locks between threads, a single CASoperation can acquire and exit locks

Cons: 偏向锁Similar to

Summary: As long as a lock is acquired by more than one thread, 偏向锁it will swell to 轻量锁.

2.3 Weight lock

If a lock is in multi-thread competition, it 轻量锁starts to spin. If the lock is not acquired after a certain number of spins, it expands to 重量锁(when there is competition, 轻量锁it will spin first, but it will eventually expand to 重量锁)

Performance: When the thread acquires 重量锁, if the acquisition fails (that is, the lock has been acquired by other threads), it will be used 自适应自旋锁, and the lock will not be acquired after a certain number of spins, and it will enter the blocking queue to wait.

Advantages: Unacquired locks enter the blocking queue, saving CPU resources. (Well, it doesn't feel like it has any advantages)

Disadvantage: 重量锁It is implemented through the monitor inside the object. The essence of the monitor is to rely on the Mutex Lock implementation of the underlying operating system. The switching between threads of the operating system requires switching from user mode to kernel mode, and the switching cost is very high. high.

Summary: As long as a lock competes with multiple threads, 轻量锁it will swell as 重量锁.

spin lock

synchronized, 轻量锁, 重量锁used 自适应自旋锁for performance optimization

First introduce自旋锁

Performance: After the thread fails to acquire the lock, it will not enter the blocking wait, but will try to acquire the lock again, and so on, until the lock is acquired, or the spin ends, it will block and wait.

Problem solved: In some scenarios, threads hold locks for a very short time. After the thread fails to acquire the lock, if the thread enters the blocking, the thread context will be switched, and the time of the context switch may be higher than the time it takes for the thread to repeatedly try to acquire the lock. At this point, the thread waits in place to repeatedly acquire the lock. Instead, it has an advantage in performance.

shortcoming:

  1. A single-core CPU has no thread parallelism, and repeated attempts will cause the process to not continue to run.
  2. Repeated attempts lead to CPU usage, and if CPU resources are tight, performance will be degraded
  3. If the lock competition time is too long, not only there is no performance improvement, but also a lot of CPU resources are wasted.

Optimization: use 自适应自旋锁. The adaptive spin lock will optimize and adjust the spin time according to the previous lock acquisition records to avoid unnecessary spins.

3. The specific synchronized process

Follow, don't get lost, this is a public account that all programs want to follow

{{o.name}}
{{m.name}}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324083586&siteId=291194637