1. Difference between synchronized and Lock
1. Hierarchy of existence
- Synchronized is a Java keyword
- Lock is an interface
2. Lock release
- Synchronized: 1) The thread that acquires the lock executes the synchronization code and releases the lock; 2) Thread execution is abnormal and the JVM tells the thread to release the lock.
- Lock: The Lock must be released in finally, otherwise thread deadlocks are likely.
3. Check whether the response is interrupted
- Synchronized can only wait for the lock to be released and cannot respond to interrupts
- Lock Waiting for a Lock can be interrupted with an interrupt() method. Once a thread has acquired the Lock, it is not interrupted by the interrupt() method. Calling the interrupt() method alone cannot interrupt a running thread, only a blocking thread.
4. Obtain the lock status
- Synchronized cannot obtain the lock status
- Lock You can use trylock to determine whether to obtain the Lock
Performance of 5.
- Low resource competition: Synchronized performance is slightly better than ReetrantLock
- When resource competition is fierce: Lock performance is much better than synchronized. Lock can improve the efficiency of multiple threads’ read operations (read and write separation is achieved through readWriteLock).
6. Scheduling mechanism
- Synchronized: uses wait, notify, and notifyAll of an Object to implement synchronization
- Lock: Uses Condition for scheduling between threads
7. Locking mechanism
- Synchronized originally uses the CPU pessimistic locking mechanism, where a thread acquires an exclusive lock.
- Lock uses optimistic locking. Optimistic locking is when an operation is performed each time without locking, assuming no conflicts, and then retry until it succeeds if it fails because of conflicts. The mechanism for optimistic locking implementation is the CAS operation.
8. Lock type
- Synchronized: reentrant, uninterruptible, and unfair
- Lock: reentrant, interrupt, fair
Two, the type of lock
1. Fair lock/unfair lock
- Fair lock: Multiple threads acquire locks in the order in which they are applied.
- Unfair lock: Multiple threads obtain locks in a different order than the one that applied for them earlier. Possibly, it could lead to priority reversals or starvation.
- ReentrantLock: The constructor specifies whether the lock is fair or not. The default is unfair. The advantage of an unfair lock is that the throughput is greater than that of a fair lock.
- Synchronized: indicates an unfair lock.
2. Reentrant lock
- Reentrant lock: when the same thread acquires the lock in the outer method, the inner method acquires the lock automatically.
- ReentrantLock and synchronized are both reentrant locks. One benefit of reentrant locks is that deadlocks are somewhat avoided.
3. Exclusive or shared lock
- Exclusive lock: This lock can only be held by one thread at a time.
- Shared lock: This lock can be held by multiple threads.
- ReentrantLock and synchronized: Exclusive lock.
- ReadWriteLock: read lock share to ensure efficient concurrent read. Write lock exclusive lock, exclusive lock and shared lock through AQS to achieve
4. Mutex/read-write lock
- Exclusive/shared lock is a broad term, and mutex/read-write lock is an implementation.
- Mutex: ReentrantLock
- Read/write lock: ReadWriteLock
5. Optimistic/pessimistic lock
Optimistic locking and pessimistic locking do not refer to specific lock types, but rather to the perspective of concurrent synchronization.
- Pessimistic lock: The concurrent operation of the same data must be modified, even if it is not modified, it will be considered modified. Therefore, for concurrent operations on the same data, pessimistic locking takes the form of locking. The pessimistic view is that concurrent operations without locking are bound to cause problems. This is suitable for scenarios where there is a lot of writing.
- Optimistic lock: It is considered that concurrent operations on the same data will not be modified. When the data is updated, the data will be updated by trying to update and constantly re – updating. Optimistically, concurrency without locking is fine. This method is applicable to scenarios where there are many read operations.
- Pessimistic locking: Used in Java, it takes advantage of various locks.
- Optimistic locking: used in Java, is lockless programming, often using CAS algorithm, a typical example is the atomic class, through CAS spin atomic operation update.
6. Block lock
- Segmented locking: this is a lock design, not a specific lock. For ConcurrentHashMap, the concurrent implementation of this lock is to implement efficient concurrent operations through segmented locking. Segment locking is designed to refine the granularity of the lock. When an operation does not need to update the entire array, only one item in the array is locked.
- The Segment lock in ConcurrentHashMap is called a Segment, which is similar to the structure of a HashMap. It contains an array of entries, and each element in the array is a linked list. It is also a ReentrantLock(the Segment inherits ReentrantLock). Lock the segment when put.
- In order to obtain the global hashMap information, we need to obtain all the segment locks in order to obtain the statistics.
7. Bias lock/lightweight lock/heavyweight lock
- These three types of locks refer to lock states and are specific to Synchronized. In Java 5, efficient Synchronized is implemented by introducing a mechanism of lock escalation. The status of these three locks is indicated by an object monitor field in the object header.
- Biased lock: a lock is automatically acquired by a thread that has been accessing a piece of synchronized code. Reduce the cost of acquiring locks. (One thread)
- Lightweight lock: When a biased lock is accessed by another thread, the biased lock will be upgraded to a lightweight lock. Other threads will try to acquire the lock in the form of spin without blocking, improving performance. (Multiple threads)
- Heavyweight lock means that when the lock is a lightweight lock, the other thread spins, but the spin will not continue. When the spin is a certain number of times, the lock has not been acquired, it will enter the block, and the lock expands to the heavyweight lock. Heavyweight locks can block other threads that apply for them and degrade performance. (Spins a certain number of times without obtaining the lock)
8. The spin lock
A spin lock is one in which the thread attempting to acquire the lock does not block immediately, but instead attempts to acquire the lock in a circular manner. This has the advantage of reducing the cost of thread context switching, but the disadvantage is that the loop consumes CPU.