Fair lock/unfair lock
Fair lock means that multiple threads acquire the lock in the order in which they apply for it Lock.
Unfair lock means that the order in which multiple threads acquire locks is not in the order in which they apply for locks. It is possible that the thread that applies later acquires the lock before the thread that applies first. It is possible that it will cause priority inversion or starvation.
For Java ReentrantLock, specify whether the lock is a fair lock through the constructor, and the default is an unfair lock. The advantage of unfair locks is that the throughput is greater than fair locks.
For Synchronized, it is also an unfair lock. Since it does not implement thread scheduling through AQS like ReentrantLock, there is no way to turn it into a fair lock.
Reentrant lock
Reentrant lock, also known as recursive lock, means that when the same thread acquires the lock in the outer method, it enters the inner method The lock will be acquired automatically. It's a bit abstract, but there is a code example below.
For Java ReentrantLock, its name can be seen as a reentrant lock, and its name is Re entrant Lock.
For Synchronized, it is also a reentrant lock. One benefit of reentrant locks is that deadlocks can be avoided to a certain 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. If it is not a reentrant lock, setB may not be executed by the current thread, which may cause a deadlock.
Exclusive lock/shared lock
Exclusive lock means that the lock can only be held by one thread at a time.
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 an exclusive lock.
The shared lock of the read lock can ensure that concurrent reading is very efficient, and the processes of reading, writing, writing, and writing are mutually exclusive.
Exclusive locks and shared locks are also implemented through AQS. Exclusive or shared locks are implemented through different methods.
For Synchronized, of course it is an exclusive lock.
Mutual Exclusion Lock/Read-Write Lock
The exclusive lock/shared lock mentioned above is a broad term, and the mutex lock/read-write lock is specific realization.
The specific implementation of mutex lock in Java is ReentrantLock.
The specific implementation of read-write lock in Java is ReadWriteLock.
Optimistic Lock/Pessimistic Lock
Optimistic lock and pessimistic lock do not refer to specific types of locks, but to the perspective of viewing concurrency and synchronization.
Pessimistic lock believes that concurrent operations on the same data will definitely be modified. Even if there is no modification, it will be considered modified. Therefore, for concurrent operations on the same data, pessimistic locking takes the form of locking. Pessimistically, I believe that concurrent operations without locking will definitely cause problems.
Optimistic locking believes that concurrent operations on the same data will not be modified. When updating data, the data will be updated by trying to update and constantly re-updating the data. Optimistically, there will be no problem with concurrent operations without locking.
We can see from the above description that pessimistic locking is suitable for scenarios with a lot of write operations, and optimistic locking is suitable for scenarios with a lot of read operations. Not locking will bring a lot of performance improvements.
The use of pessimistic locking in Java is to use various locks.
The use of optimistic locking in Java is lock-free programming, and the CAS algorithm is often used. A typical example is the atomic class, which implements the update of atomic operations through CAS spin.
Segmented lock
Segmented lock is actually a lock design, not a specific lock. For ConcurrentHashMap, its concurrency implementation is Efficient concurrent operations are achieved through segmented locks.
Let’s take ConcurrentHashMap to talk about the meaning and design ideas of segment lock. The segment lock in ConcurrentHashMap is called Segment, which is similar to the structure of HashMap (the implementation of HashMap in JDK7 and JDK8), that is, the internal There is an Entry array, and each element in the array is a linked list; it is also a ReentrantLock (Segment inherits ReentrantLock).
When it is necessary to put an element, it does not lock the entire hashmap, but first uses the hashcode to know which segment it should be placed in, and then locks this segment, so when When multi-threaded put, as long as it is not placed in a segment, true parallel insertion is achieved.
However, when counting size, when obtaining the global information of hashmap, you need to obtain all segment locks to count.
The design purpose of segmented lock is to refine the granularity of the lock. When the operation does not need to update the entire array, only one item in the array is locked.
Bias lock/Lightweight lock/Heavyweight lock
These three types of locks refer to the status of the lock and are for Synchronized. In Java 5, efficient Synchronized is achieved by introducing a lock upgrade mechanism. The status of these three locks is indicated by the fields in the object header of the object monitor.
Biased lock means that a piece of synchronization code is always accessed by one thread, then the thread will automatically acquire the lock. Reduce the cost of acquiring locks.
Lightweight lock means that when the lock is a biased lock and is accessed by another thread, the biased lock will be upgraded to a lightweight lock. Other threads will try to acquire the lock through spin without blocking. , improve performance.
Heavyweight lock means that when the lock is a lightweight lock, although another thread is spinning, the spin will not continue forever. When it spins a certain number of times, it has not been acquired yet. lock, it will enter blocking, and the lock will expand into a heavyweight lock. Heavyweight locks will block other application threads and reduce performance.
Spin lock
In Java, a spin lock means that the thread trying to acquire the lock will not block immediately, but will use a loop to try to acquire the lock. , the advantage of this is to reduce the consumption of thread context switching, but the disadvantage is that the loop consumes CPU.
php Chinese website, a large number of free Java introductory tutorials, welcome to learn online!
The above is the detailed content of What are the differences between java locks?. For more information, please follow other related articles on the PHP Chinese website!