In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you how to achieve fair lock and unfair lock in Java. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Fair lock
Fair lock means that multiple threads acquire the lock according to the order in which the lock is applied, and the thread directly enters the queue so that the first thread in the queue can acquire the lock. The advantage of fair locking is that the thread waiting for the lock will not starve to death. The disadvantage is that the overall throughput efficiency is relatively low, all threads except the first thread in the waiting queue will block, and the overhead of CPU waking up blocking threads is higher than that of unfair locks.
Unfair lock
An unfair lock is when multiple threads add a lock and directly attempt to acquire the lock. If not, they will wait at the end of the queue. However, if the lock is just available at this time, the thread can acquire the lock without blocking, so it is possible for the thread applying for the lock to acquire the lock first after the unfair lock occurs. The advantage of unfair locking is that it can reduce the overhead of arousing threads, and the overall throughput efficiency is high, because threads have the chance to acquire locks directly without blocking, and CPU does not have to wake up all threads. The disadvantage is that threads in the waiting queue may starve to death or wait a long time to acquire the lock.
The above explanation may have the advantage of abstraction. Let's use a picture to distinguish what is a fair lock. What is an unfair lock?
As shown in the picture above, suppose there is a well guarded by a caretaker, and the administrator has a lock. Only the person who gets the lock can draw water, and the lock should be returned to the administrator after getting the water. Everyone who comes to fetch water needs the permission of the administrator and gets the lock before they can fetch water. if someone in front is fetching water, then the person who wants to fetch water must stand in line. The administrator will check to see if the next person who wants to fetch water is at the front of the line, and if so, he will give you a lock to fetch water; if you are not the first person in line, you must line up at the end of the line, which is a fair lock.
But for unfair locks, the administrator has no requirements for the person who draws the water. Even if there are people waiting in the queue, if the last person has just finished fetching the water and returned the lock to the administrator and the administrator has not allowed the next person in the queue to fetch water, there happens to be a queue jumper, the queue jumper can get the lock directly from the administrator to fetch water, there is no need to queue, the person who was originally waiting in the queue can only continue to wait. As shown in the following figure (or an example of fetching water):
After a thorough understanding of fair locks and unfair locks in Java, let's take a look at ReentrantLock in Java.
Anyone who has read the ReentrantLock source code knows that there is an inner class Sync,Sync inheriting AQS (AbstractQueuedSynchronizer), and most of the operations to add and release locks are actually implemented in Sync. It has two subclasses: fair lock FairSync and unfair lock NonfairSync. ReentrantLock uses unfair locks by default, or it can be specified by constructors to use fair locks.
Let's take a look at the source code of the locking methods for fair and unfair locks:
Through the comparison of the source code in the figure above, we can clearly see that the only difference between the lock () method of fair locks and unfair locks is that fair locks have one more constraint when acquiring synchronization state: hasQueuedPredecessors ().
Going back to hasQueuedPredecessors (), you can see that the method mainly does one thing: it mainly determines whether the current thread is at the first in the synchronization queue. If so, return true, otherwise return false.
To sum up, the fair lock is to realize the fair characteristic that multiple threads acquire the lock according to the order of applying for the lock by synchronizing the queue. When adding an unfair lock, we do not consider the problem of queuing and wait, but directly try to acquire the lock, so there is a case that the lock is obtained first after the application.
The above content is how to achieve fair lock and unfair lock in Java. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.