In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what locks are there in Java". The content of the explanation 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 locks are in Java.
Fair lock / unfair lock
Fair lock means that multiple threads acquire the lock in the order in which the lock is applied for. Unfair lock means that multiple threads do not acquire locks in the order in which they apply for locks, and it is possible that the thread that applies later takes precedence over the thread that applies first. It is possible that it can cause priority reversal or hunger. For Java ReentrantLock, the constructor specifies whether the lock is a fair lock, and the default is an unfair lock. The advantage of unfair locks is that the throughput is larger than fair locks. For Synchronized, it is also an unfair lock. Unlike ReentrantLock, which implements thread scheduling through AQS (AbstractQueuedSynchronizer), there is no way to make it a fair lock.
Reentrant lock
A reentrant lock, also known as a recursive lock, means that when the same thread acquires the lock in the outer method, the inner method automatically acquires the lock. It's a little abstract, and here's an example of the code. For Java ReentrantLock, his name shows that it is a reentrant lock, and its name is ReentrantLock re-entry lock. For Synchronized, it is also a reentrant lock. One of the advantages of reentrant locks is that deadlocks can be avoided to some extent.
Synchronized void setA () throws Exception {Thread.sleep (1000); setB ();}
Synchronized void setB () throws Exception {Thread.sleep (1000);}
The above code is a feature of a reentrant lock, and if it were not for the reentrant lock, the setB might not be executed by the current thread, which could cause a deadlock.
Exclusive lock / shared lock exclusive lock means that the lock can only be held by one thread at a time. A shared lock means that the lock can be held by multiple threads.
For Java ReentrantLock, it is an exclusive lock. But for another implementation class of Lock, ReadWriteLock, its read lock is a shared lock and its write lock is exclusive. The shared lock of the read lock ensures that concurrent reads are very efficient, and the processes of reading and writing are mutually exclusive. Exclusive lock and shared lock are also realized through AQS, through the implementation of different methods to achieve exclusive or shared. For Synchronized, it is, of course, an exclusive lock.
Mutex / read-write lock
The exclusive lock / shared lock mentioned above is a broad term, and the mutex / read-write lock is the concrete implementation.
The concrete implementation of mutex in Java is ReentrantLock read-write lock in Java, which is ReadWriteLock optimistic lock / pessimistic lock.
Optimistic and pessimistic locks do not refer to specific types of locks, but to the perspective of concurrent synchronization.
Pessimistic lock believes that the concurrent operation of the same data must be modified, even if there is no modification, it will be considered modified. Therefore, for the concurrent operation of the same data, the pessimistic lock takes the form of locking. Pessimistically, unlocked concurrency operations are bound to go wrong. Optimistic locks assume that concurrent operations on the same data will not be modified. When you update the data, you will try to update the data and update the data again and again. Optimistically, there is nothing wrong with unlocked concurrent operations.
From the above description, we can see that pessimistic locks are suitable for scenarios with a large number of write operations, while optimistic locks are suitable for scenarios with a large number of read operations, and not locking will bring a lot of performance improvement. The use of pessimistic locks in Java is the use of various locks. The use of optimistic locks in Java is lock-free programming, often using the CAS algorithm. A typical example is the atomic class, which updates atomic operations through CAS spin.
Sectional lock
Segmented lock is actually a kind of lock design, not a specific kind of lock. For ConcurrentHashMap, the realization of concurrency is to achieve efficient concurrent operation in the form of segmented lock.
Let's talk about the meaning and design idea of segmented lock in ConcurrentHashMap. The segmented lock in ConcurrentHashMap is called Segment, which is similar to the structure of HashMap (the implementation of HashMap in JDK7 and JDK8), that is, there is an internal Entry array, and each element in the array is a linked list; at the same time, it is also a ReentrantLock (Segment inherits ReentrantLock).
When the put element is needed, instead of locking the entire hashmap, we first know which segment it is going to put through hashcode, and then lock the segment, so when multithreaded put, as long as it is not placed in a segment, real parallel insertion is achieved.
However, when you count size, you need to get all the segmented locks when you get the global information of hashmap.
The purpose of the segmented lock is to refine the granularity of the lock, and when the operation does not need to update the entire array, the locking operation is performed on only one item in the array.
Bias lock / lightweight lock / heavyweight lock
These three locks refer to the state of the lock and are specific to Synchronized. Efficient Synchronized is realized by introducing lock upgrade mechanism in Java 5. The status of these three locks is indicated by the fields of the object monitor in the object header.
A biased lock means that a piece of synchronization code has been accessed by a thread, then the thread automatically acquires the lock. Reduce the cost of acquiring locks. Lightweight lock means that when the lock is biased and accessed by another thread, the biased lock will be upgraded to a lightweight lock, and other threads will try to acquire the lock in the form of spin without blocking and improving performance. A heavyweight lock means that when the lock is a lightweight lock, although the other thread is spinning, the spin will not last forever. When the lock is not acquired for a certain number of spins, it will enter the blocking, and the lock will expand into a heavy lock. Heavyweight locks can cause other requesting threads to enter blocking and slow performance. Spin lock
In Java, spin lock means that the thread trying to acquire the lock does not block immediately, but uses a loop to try to acquire the lock, which has the advantage of reducing the consumption of thread context switching, while the disadvantage is that the loop consumes CPU.
Thank you for your reading, these are the contents of "what locks are there in Java". After the study of this article, I believe you have a deeper understanding of what locks are in Java, 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.