Lock learning java

Lock from the design concept can be divided into two categories, namely pessimistic lock (mutex) and optimistic locking (non-mutex)
pessimistic lock is suitable for write once read many small scenes, optimistic locking applies to read how much of the writing scene
java in the pessimistic lock is locked in Synchronized, AQS framework is the first attempt to acquire cas optimistic locking lock, get less, it will be converted to a pessimistic lock, such as RetreenLock.
java lock There are two main implementations, respectively (under lock Lock java.util.concurrent.locks packet interface, etc.) (Synchronized keywords) and JDK jvm virtual machine implementation codes implemented
Synchronized a lock is achieved kind of mutex (a maximum of only one thread holds the lock, a thread when another thread holding the lock can not be locked into the area), it is a pessimistic locking
synchronized first and only heavyweight lock in jdk1.6 in for Synchronized optimized.
The compiler level, eliminating the use of locks (for some unnecessary, does not cause security problems cancel synchronization code synchronization) and lock coarsening (the code for those last synchronization code synchronization is performed multiple times and they can be and to )
while the introduction of adaptive spin locks, lock bias, lightweight lock, lock these three belong to the optimistic lock. jdk1.6 is enabled by default biased locking lock and lightweight
Java SE1.6 lock in a total of four states, no lock status, tend to lock status, lock status lightweight and heavyweight lock status, it will be with competition escalated.
Lock can upgrade but can not downgrade, upgrade but can not downgrade this lock strategy is designed to improve efficiency and get the lock to release the lock.

偏向锁:(Mark Word 标志位为01时)线程首次进入偏向锁时,记录当前线程ID,
等到下一次执行该方法时,判断如果线程ID一致,就直接执行,啥也不做。只有第一次执行时需要CAS机制来设置。
如果再次执行该方法时,线程ID与原先保存的不一致,则撤销偏向后恢复到未锁定(标志位为‘01’)或轻量级锁定(标志位为‘00’)的状态。(偏向锁适用于那种,始终只有一个线程在执行一个方法的情况)

轻量级锁:(Mark Word 标志位为01时),虚拟机会用CAS操作去尝试将对象的Mark Word更新为指向Lock Record的指针,并将Lock record里的owner指针指向object mark word,
如果更新成功,则状态位改为‘00’,进入到轻量级锁定状态,
如果更新失败,首先会检查对象的Mark Word是否指向当前线程的栈,如果是说明当前对象已经持有锁了,则直接进入同步块执行,
否则说明该锁被别的线程占用,轻量级锁就要膨胀为重量级锁,锁标志的状态值变为“10”,Mark Word中存储的就是指向重量级锁(互斥量)的指针,后面等待锁的线程也要进入阻塞状态。
 而当前线程便尝试使用适应性自旋锁来获取锁,自旋就是为了不让当前线程立刻阻塞,从而使CPU从用户态转为核心态。而采用循环等待一段时间去获取锁的过程。
轻量级锁解锁通过CAS操作尝试把线程中复制的Displaced Mark Word对象替换当前的Mark Word。替换成功,同步过程完成,替换失败说明其他线程尝试获取过该锁,在释放锁的同时要唤醒被挂起的线程

ps: Lock small note
Synchronized only non-fair locks, RetreenLock () and RetreenLock (false) non-equity locks, RetreenLock (true) is fair locks
after JDK1.6 give priority to the use of synchronized for synchronization
difference RetreenLock and Synchronized of
1, ReentrantLock support interrupt threads waiting to acquire a lock, while waiting for a mutex created by the synchronized, would have been blocked, can not be interrupted.
2, synchronized release of the lock must be acquired in the lock block of code, but can be more flexible ReentrantLock choose when to release the lock. (Do not forget to release a lock)
3, ReentrantLock can achieve fair and unfair lock lock
4, ReentrantLock can bind multiple conditions, a ReentrantLock object can bind multiple Condition objects (condition variable or condition queue) at the same time,
while in the synchronized , wait () and notify () lock objects or notifyAll () method can implement an implicit condition, but if and when more than one of the conditions associated with it, will have to add an extra lock
ReentrantReadWriteLock read-write lock
to read when adding a read lock, write when write-lock, read lock is a shared lock, write lock is an exclusive lock
atomic atomic classes are using the CAS implementation
CopyOnWriteArrayList
read no effect when writing copy a CopyOnWriteArrayList, then modify, change complete after the object pointer to CopyOnWriteArrayList new copy, read and write separation of thought

Guess you like

Origin www.cnblogs.com/youmingDDD/p/10961643.html