JAVA-related lock

The concept of locks in JAVA
spin lock:
is when one thread acquires the lock time, if the lock has been acquired by another thread, the thread will wait for the cycle, and then continue to determine whether the lock can be successfully retrieved, until get to lock will exit the loop.
Optimistic locking:
assuming that there is no conflict, and if found inconsistent data and previously acquired, then read the latest data when data is modified, the modified retry modified.
Pessimistic locking:
suppose will happen concurrent conflict, synchronize all related operations on the data, the data read from the start locked.
Exclusive lock (write):
to add resources to write lock, the thread can modify resources, other threads can not lock (Write Once)
contribution lock (read):
to add resources after a read lock can not only change, but also other threads plus lock can read, can not write-lock (read more)
reentrant lock, non-reentrant lock:
after the thread got a lock, which can freely enter the other code in the same lock synchronized.
Fair locks, lock unfair:
the order of competition for the lock, according to first come first served, for the fair.

Keywords synchronization the synchronized:
1. A method for instance, when the static side 4. The method, specified implicitly lock object
2. For the code block, display the specified lock object
3. The lock Scope: object lock, lock type, distributed lock
Features: reentrant, exclusive, pessimistic locking
lock optimization: locks elimination (open lock cancellation parameters: -XX: + DoEscapeAnalysis -XX: + EliminateLocks)
lock coarsening: JDK do lock coarsening of optimization, but we can in the code-level optimization

Biased lock:
Here Insert Picture Description
Lock upgrade:
Here Insert Picture Description

Published 16 original articles · won praise 1 · views 465

Guess you like

Origin blog.csdn.net/qq_32550623/article/details/104078552