In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you how to understand the ReentrantLock in the java concurrent package. The content is concise and easy to understand, and it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
As a popular language, java itself supports concurrency very well. The concurrent package in JDK has a series of concurrency tools to help developers build a high concurrency system quickly. Among them, there is an indispensable interface java.util.concurrent.locks.Lock related to locks in concurrent development, and a commonly used implementation class of this interface is ReenTrantLock. The following in-depth analysis of the implementation of the lock.
First of all, what are the characteristics of the lock?
1. Reentrant:
The lock is a reentrant lock. What do you mean? That is, if the current thread has acquired the lock, it will immediately acquire success instead of deadlock when it acquires the lock again, that is, the same thread can lock multiple times, and there will be a state record at the same time of locking. Record how many times the current thread has locked, and each time it is released, the state will be reduced by 1. Is it possible to lock indefinitely until the state is reduced to 0 to indicate that the lock has been released by the thread (so how many times the lock is repeated)? The answer is no, the state is a value of type int, and each repeated lock is incremented by 1, which means that the maximum number of repeated locks for the same thread is equal to the maximum positive integer that int can represent. What if the number of locks exceeds the maximum? When the number of locks exceeds this maximum, the system will throw an Error, yes, not an exception, but an error error! The specific code is as follows:
In the code, when nextc is less than 0, overflow occurs and Error is thrown.
Second, do not respond to interrupts:
Although the lock () method of the Lock interface is a method that may be blocked (the current thread needs to block and wait when other threads hold the lock), this method does not respond to interrupts like the sleep method during blocking, that is, if you try to interrupt the thread using the Thread.interrupt () method during lock () method blocking, the thread will not respond. But when the thread acquires the lock, you can use the Thread.interrupted () method to get the interrupt state of the thread, which will get that the interrupt state of the thread is true.
Third, the timeout can be exceeded:
The traditional synchronize lock can only wait indefinitely after another thread has acquired the lock, and the thread can not continue until the other thread releases the lock, while Lock's tryLock method allows immediate response, that is, if it fails to acquire the lock, it immediately returns a false indicating that it failed to acquire the lock or to return a false after waiting for a period of time to acquire the lock. This approach means that if the user has not acquired the lock for a long time, he can try to go back and do some other processing instead of waiting here to acquire the lock, which is useful in some scenarios.
In the specific details, there are two implementations inside the lock, namely, fair lock and unfair lock, so what is the difference between unfair lock and fair lock? Where is the fairness reflected?
Fairness is mainly reflected in the operation when trying to acquire a lock. When trying to acquire a lock, Fair Lock will first determine whether other threads are already waiting, and if not, it will try to acquire the lock directly. Otherwise, it will join the queue and wait for the previous thread to release the lock before it will attempt to acquire the lock.
On the other hand, the unfair lock directly attempts to acquire the lock regardless of whether there is already a thread waiting, and the acquisition fails to join the queue and wait for the previous thread to release the lock.
(PS: both the above fair lock and unfair lock will determine whether the current thread holding the lock is the local thread after a failed direct attempt to acquire it, and if so, it will also be successful.)
The unfair lock attempts to get the lock part of the code:
The fair lock attempts to get the lock part of the code:
The difference between fair locks and unfair locks can be clearly seen from the source code.
Finally, when to use Lock and when to use synchronize?
It is recommended that Lock be used if fine-grained lock control is needed, or when concurrency is expected to be low, otherwise synchronize should be given priority. The performance of synchronize is better than that of Lock in high concurrency scenarios, and the deadlock of synchronize can be detected and monitored by the monitoring function when deadlock occurs. at the same time, the performance of synchronize has been greatly improved in the latest version, and the actual performance is not lower than that of Lock. (the 1.8 currently used by the author, and has been tested using several simple scenarios) So if there are not the above needs or other special needs can only be met by Lock, priority should be given to using synchronize, but Lock also provides us with a good idea and can be regarded as a trend. As for why concurrency scenarios have existed for a long time, while Lock is only available in newer java? It is mainly because Lock relies on CAS operations, while CAS atomic operations have hardware support later, so there is Lock at the language level after the hardware supports CAS operations.
What are the characteristics of Java? what are the characteristics of Java? what is the 1.Java language that represents the static object-oriented programming language? it implements the object-oriented theory and allows programmers to carry out complex programming in an elegant way of thinking. 2.Java has the characteristics of simplicity, object-oriented, distributed, security, platform independence and portability, dynamic and so on. 3. Using Java, you can write desktop applications, Web applications, distributed systems, embedded system applications, and so on.
The above is how to understand the ReentrantLock in the java concurrent package. 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.