Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the Java spin lock?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail what the Java spin lock is, and the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Foreword:

Blocking or waking up a CPU thread requires the operating system to switch between Java states, which takes processor time. If the content in the synchronized code block is too simple, the state transition may take longer than the user code execution time.

1. Spin lock

In some scenarios, the locking time of synchronous resources is very short, and for this short period of time to switch threads, the cost of thread suspending and restoring the scene may outweigh the loss of the system.

If the machine has multiple CPU cores and allows two or more threads to execute in parallel at the same time, we can ask the later thread requesting the lock not to give up the CPU execution time and see if the thread holding the lock will release the lock soon.

In order for the current thread to "wait a minute", we need to make the current thread spin. If the thread that locked the synchronization resource before the spin completes has released the lock, then the current thread can acquire the synchronization resource directly without blocking, thus avoiding the overhead of switching threads. This is the spin lock.

two。 Work flow

3. Shortcoming

Spin lock itself is flawed, it is not a substitute for blocking. Spin waiting avoids the overhead of thread switching, but it takes up processor time.

If the lock is occupied for a short time, the spin waiting effect will be very good.

If the lock is occupied for a long time, the spinning thread will only waste processor resources.

Therefore, the spin wait time must be limited, and if the spin exceeds the limit number of times and does not successfully acquire the lock, the thread should be suspended. (this number is 10 by default and can be configured)

4. Realization principle

The realization principle of spin lock is also that the do-while loop in the source code that calls unsafe for self-increment operation in CAS,AtomicInteger is a spin operation. If you fail to modify the value, you will perform the spin through the loop until the modification is successful.

Public final int getAndAddInt (Object var1, Long var2, int var4) {int var5; do {var5 = this.getIntVolatile (var1, var2);} while (! this.compareAndSwapInt (var1, var2, var5, var5 + var4)); return var5;} 5. Adaptive spin

Adaptation means that the spin time (number of times) is not fixed, but is determined by the spin time of the previous time on the same lock and the state of the lock's owner.

If, on the same lock object, spin wait has just successfully acquired the lock, and the thread holding the lock is running, the virtual machine will think that the spin is likely to succeed again, which in turn will allow the spin wait to last relatively longer.

If spin is rarely successfully acquired for a lock, it is possible to omit the spin process when trying to acquire the lock later, blocking the thread directly and avoiding a waste of processor resources.

About what the Java spin lock is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report