Synchronized: 1. Synchronized is an optimistic lock, but also a pessimistic lock (see the level of lock competition gradually escalating) 2. Synchronized is not a read-write lock, but an ordinary mutual exclusion lock 3. Synchronized is both a lightweight lock and a heavyweight Lock (see the level of lock competition gradually escalating) 4. Synchronized is a spin lock at the beginning (spin lock is also a lightweight lock), and then gradually upgrades 5. Unfair lock 6. Reentrant lock
The principle of synchronized: 1. The characteristics of synchronized Atomicity Memory visibility Orderedness (instruction reordering) Unfair locks Reentrant locks cannot be interrupted (not that the thread cannot be interrupted, but that when the lock cannot be obtained, this The blocking process cannot be interrupted) 2. The use of synchronized Modifies code blocks Modifies common methods Modifies static methods 3. Synchronized lock mechanism Lock upgrade, lock elimination, lock coarsening 4. Synchronized implementation obtains monitor Every object has a monitor associated monitor => monitor lock
Synchronized lock upgrade: no lock -> biased lock -> spin lock (lightweight lock) -> heavyweight lock
Biased lock: It is not a real lock. It means that there is a certain bias towards a certain thread. The biased lock is a sign. In the object header, the thread to which the current lock is biased is stored. When a thread accesses, it will first judge whether this sign is equal to the current one. If the thread is equal to the current thread, then the lock will be obtained directly. If it is empty, the object header of the current lock will be set to itself. If it is not equal to itself, the lock will be upgraded.
Common locking strategies:
Optimistic lock and pessimistic lock Optimism and pessimism are a kind of attitude of operating data Optimism: think that this data is not changed, so no lock is added, before modification, judge whether other threads have modified the data, and modify it without modification , if it is modified, stop the operation on the data. Pessimistic: think that this data must have threads to operate, so before the operation, it will be locked first. Optimistic lock and pessimistic lock are not good or bad, and there are suitable scenarios. Optimistic lock: suitable for reading and writing Less scenarios, more reads (no conflicts) pessimistic locks: suitable for scenarios with more writes and fewer reads, more data is written (more conflicts) synchronized is both optimistic locks and pessimistic locks . Optimistic locks, as the frequency of conflicts increase, gradually become pessimistic locks
The operation of the read-write lock
on data is mainly reading and writing
1> two threads, all read operations on data => thread safety
2> two threads, one read, one write => thread is not safe
as long as there is writing, the thread is Unsafe
For this reason, read-write locks appear:
read-write locks, locks on data operations are divided into read locks and write locks.
For data, read operations, add read locks
to write operations, add write locks
to read locks + read Locks are not mutually exclusive.
Read lock + write lock are mutually exclusive.
Write lock + read lock are mutually exclusive.
Synchronized is an ordinary mutual exclusion lock, not a read-write lock
Heavyweight locks and lightweight locks
are relative
heavyweight locks, which do more, and the performance is slower
Lightweight locks, do less, and the performance is faster
Most optimistic locks are lightweight locks
Pessimistic locks are weight class lock is not absolute
Most heavyweight locks need to call mutexes.
Lightweight locks generally do not use
synchronized. At first, they are lightweight locks. As conflicts increase, they will gradually be upgraded to heavyweight locks.
Spin locks and pending wait locks
Pseudo code of spin lock: non-real code
while (lock grab (lock) == failure) {} If the lock grab fails, it will continue to loop until the lock grab is successful.
Synchronized is also a spin lock at the beginning
Fair lock and unfair lock
synchronized is an unfair lock
Fair lock: first come first
served Unfair lock: Whoever grabs whose
unfair lock is generally better than fair lock
Reentrant lock and non-reentrant lock
synchronized is a reentrant lock, that is, it will not lock itself to death
Non-reentrant lock: it will lock itself