1. Can be re-entered lock
Synchronized and lock are reentrant locks
Indicates that the lock allocation mechanism is thread-based, not method-based
For example, when another synchronization method is called in one of the synchronization methods, and then the second synchronization method is entered, no need to re-request the lock
2. Interruptible Lock
Synchronized is non-disruptive.
One thread has acquired a lock on an object, and another thread wants to acquire the lock on that object, and must wait until the first thread releases the lock (execution, exception)
Lock is interruptible when it calls the Lockinterruptibly method to obtain a lock.
A thread has acquired a lock on an object, another thread wants to get the lock on that object, waits while waiting, and waits for the interrupt method terminal to call the thread during the wait.
3. Fair lock, non-fair lock
Fair Lock: Get locks in order of request
Unfair lock: does not guarantee the order of acquiring locks, high throughput, causing priority reversal or starvation (some threads have been waiting)
Synchronized Non-fair lock
Lock implementation class: Reentrantlock It is a non-fair lock by default, and when the object is created, an overloaded constructor is used, the passed-in parameter (TRUE), is set to a fair lock
4. Shared lock, exclusive lock
Shared locks: Locks can be held by multiple threads
Exclusive lock: The lock can only be held by one thread
Synchronized and lock are exclusive locks.
Readwritelock (read-write lock), its read lock is a shared lock, write lock is an exclusive lock
Implementing Class Reentrantreadwritelock
5. Optimistic lock, pessimistic lock
Optimistic Lock: Concurrent operation on the same data, optimistic that no modification will occur, when updating the data, try to update (version)
Pessimistic Lock: The concurrent operation of the same data, pessimistic that must be modified, lock
Optimistic Lock: CAS
6. Segmented lock
Segment lock is a lock design (CONCURRENTHASHMAP)
7. Bias lock/Lightweight lock/Heavyweight lock
jdk1.5 introduces the lock escalation mechanism to achieve efficient synchronized
Biased lock: If one of the synchronization code is accessed by a thread, the thread acquires the lock automatically
Lightweight Lock: When the lock is biased, another thread accesses the synchronization code, the bias lock rises to a lightweight lock, and the other thread acquires the lock in the spin form, without blocking
Heavy lock, Lock is a lightweight lock, another thread spins a certain number of times after the lock is still not acquired, blocking, lightweight lock up to a heavyweight lock
8. Spin Lock
The thread attempting to acquire the lock does not block immediately, but instead attempts to acquire the lock in a circular way, reducing the consumption of the context switch, but the loop consumes the CPU
Locks in Java