[Java Concurrent Programming] summary of synchronized related interview questions

Tell me about your understanding of the synchronized keyword

The synchronized keyword is used to solve the synchronization of accessing resources between multiple threads. The synchronized keyword can ensure that only one thread can execute the modified method or code block at any time.

It is worth noting that in the early days of Java, jdk1 Before 6, synchronized was a heavyweight lock with low efficiency.

The reason is:

However, jdk1 After 6, Java officials have greatly optimized the synchronized keyword from the JVM level, and the efficiency is not comparable. The main optimizations include spin lock, adaptive spin lock, lock elimination, lock coarsening, bias lock, lightweight lock and other technologies to reduce the overhead of lock operation.

Three uses of synchronized keyword

Underlying principle of synchronized keyword

By right Class file decompilation can find:

Although the implementation details of the two are different, they are essentially synchronized by the JVM based on entering and exiting the monitor object. The requirements of the JVM are as follows:

JDK1. After 6, optimize the synchronized keyword

https://blog.csdn.net/qq_34337272/article/details/108498442

Optimization: bias lock, lightweight lock, spin lock, adaptive spin lock, lock elimination, lock coarsening.

Composition of Java object headers

The lock exists in the Java object header. The components of the object header are:

Composition of markword

The mardword of the Java object header records the information related to the object and lock. In the unlocked state, the hashcode, generation age and lock mark bit of the object are stored in the mark word of the Java object header by default. In a 64 bit JVM, mark word is 64 bit.

During operation, the data stored in mark word will change with the change of lock flag bit. The function of lock upgrade is also mainly completed by the lock flag bit and whether it is biased to the lock flag in markword.

Lock upgrade process

Lock upgrade process: no lock, bias lock, lightweight lock and heavyweight lock

Bias lock

Applicable scenarios of bias lock

Biased locking is mainly used for optimization: the same thread applies for the same lock multiple times. In some cases, the same thread competes for lock resources most of the time.

Biased locking

Main process: when a thread accesses the synchronization block and obtains the lock, the thread ID of lock bias will be stored in the lock record in the object header and stack frame. In the future, the thread does not need CAS operation to lock and unlock when entering and exiting the synchronization block. Just simply test whether the bias lock pointing to the current thread is stored in the mark word of the object header.

Cancellation of bias lock

Once other threads compete for lock resources, the biased lock will be revoked. The revocation of bias lock may need to wait for the global security point [there is no bytecode being executed at this time point].

Closing of deflection lock

Bias lock is enabled by default in Java 6 and Java 7, but it is activated only a few seconds after the application starts. If necessary, you can use the JVM parameter to turn off the delay: - XX: biasedlockingstartupdelay = 0.

If the lock is usually in the contention state, you can enter the lightweight lock state through - XX: - usebiasedlocking = false.

Lightweight Locking

If the biased lock exists, if another thread competes for the lock, and the thread ID in the object header markword is different from the current thread ID, the thread will try to obtain the lock through CAS operation. If the acquisition fails, it indicates that there is competition for the lock, and the biased lock will be upgraded to a lightweight lock.

Locking of lightweight lock

Unlocking of lightweight lock

Applicable scenarios for lightweight locks

Threads execute synchronization blocks alternately, and most locks do not compete for a long time in the whole synchronization cycle.

Comparison of advantages and disadvantages of lock

The difference between synchronized keyword and reentrantlock

common ground

difference

reference material

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>