In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what is the difference between Binary Semaphore and Reentrant Lock". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the difference between Binary Semaphore and Reentrant Lock".
1. Introduction
Discusses binary semaphores (Binary Semaphore) and reentrant locks (Reentrant Lock).
two。 What is a binary semaphore
Binary semaphores provide a signaling mechanism for access to a single resource. In other words, binary semaphores provide a mutex mechanism that allows only one thread to access one critical part at a time. It retains only one pass, so binary semaphores have only two states: available (count=1) and unavailable (count=0).
We use the Semaphore class in Java to discuss the implementation of a simple binary semaphore:
Semaphore binarySemaphore = new Semaphore (1); try {binarySemaphore.acquire (); assertEquals (0, binarySemaphore.availablePermits ());} catch (InterruptedException e) {e.printStackTrace ();} finally {binarySemaphore.release (); assertEquals (1, binarySemaphore.availablePermits ());}
Here, we can observe that the acquire method reduces the available licenses by one. Similarly, the release method increases the available licenses by 1.
In addition, the Semaphore class provides the fairness parameter. When set to true, the fairness parameter ensures the order in which requesting threads obtain permissions (based on their wait time):
Semaphore binarySemaphore = new Semaphore (1, true)
3. What is a reentrant lock?
A reentrant lock is a mutex that allows a thread to reenter a lock on a resource (multiple times) without a deadlock.
The thread that enters the lock increases the hold count one at a time. Similarly, the hold count is reduced when unlocking is requested. Therefore, the resource is locked until the counter returns to zero. For example, let's look at a simple implementation that uses the ReentrantLock class in Java:
ReentrantLock reentrantLock = new ReentrantLock (); try {reentrantLock.lock (); assertEquals (1, reentrantLock.getHoldCount ()); assertEquals (true, reentrantLock.isLocked ());} finally {reentrantLock.unlock (); assertEquals (0, reentrantLock.getHoldCount ()); assertEquals (false, reentrantLock.isLocked ());}
Here, the lock method increases the hold count by 1 and locks the resource. Similarly, the unlock method reduces the hold count and unlocks the resource if the hold count is zero. When a thread re-enters the lock, it must request the same number of unlocks to release resources:
ReentrantLock.lock (); reentrantLock.lock (); assertEquals (2, reentrantLock.getHoldCount ()); assertEquals (true, reentrantLock.isLocked ()); reentrantLock.unlock (); assertEquals (1, reentrantLock.getHoldCount ()); assertEquals (true, reentrantLock.isLocked ()); reentrantLock.unlock (); assertEquals (0, reentrantLock.getHoldCount ()); assertEquals (false, reentrantLock.isLocked ())
Similar to the Semaphore class, the ReentrantLock class also supports the fairness parameter:
ReentrantLock reentrantLock = new ReentrantLock (true)
4. Binary semaphore and reentrant lock
4.1. Mechanism
Binary semaphore is a signaling mechanism, while reentrant lock is a locking mechanism.
4.2. Ownership
No thread is the owner of the binary semaphore. However, the last thread that successfully locks the resource is the owner of the reentrant lock.
4.3. Essence
Binary semaphores are essentially non-reentrant, which means that the same thread cannot reacquire critical parts, otherwise it will lead to a deadlock. On the other hand, reentrant locks essentially allow the same thread to reenter the lock multiple times.
4.4. Flexibility
Binary semaphores provide a higher level of synchronization mechanism through custom implementations that allow locking and deadlock recovery. As a result, it provides more control for developers. However, the reentrant lock is a low-level synchronization with a fixed locking mechanism.
4.5. Modifiable
Binary semaphores support operations such as wait and signal (obtained and released in Java's Semaphore class) to allow any process to modify available licenses. On the other hand, only the same thread that locks / unlocks the resource can modify the reentrant lock.
4.6. Deadlock recovery
Binary semaphores provide a non-proprietary release mechanism. Therefore, any thread can release a deadlock recovery license for binary semaphores.
On the contrary, it is difficult to achieve deadlock recovery in the case of reentering the lock. For example, if the owner thread that can reenter the lock goes to sleep or waits indefinitely, it is not possible to release resources, resulting in a deadlock condition.
Thank you for your reading, the above is the content of "what is the difference between Binary Semaphore and Reentrant Lock". After the study of this article, I believe you have a deeper understanding of the difference between Binary Semaphore and Reentrant Lock, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.