java中的锁

synchronized 获取的锁,在方法抛出异常的时候会自动解锁 

ReentrantLock 获取的锁,异常的时候也不会自动释放! 

调用wait()时,会释放锁。

调用sleep()时,不会释放锁。

 

 

Lock接口

api:lock(), unlock()....

AbstractQueuedSynchronizer队列同步器

同步器的设计是基于模板方法模式的,也就是说,使用者需要继承同步器并重写指定的方法,随后将同步器组合在自定义同步组件的实现 中, 并调用同步器提供的模板方法,而这些模板方法将会调用使用者重写的方法。

可重写的方法:

protected boolean tryAcquire(int arg)

protected boolean tryRelease(int arg)

protected int tryAcquireShared(int arg)

protected boolean tryReleaseShared(int arg)

protected boolean isHeldExclusively()

在重写的方法中,需要对同步进行更改时,可以使用同步器提供的三个方法来安全地改变同步状态:

getState()

setState()

compareAndSetState()

重入锁

读写锁

LockSupport

Condition接口

posted @ 2017-03-01 13:23  idea偶买噶  阅读(167)  评论(0编辑  收藏  举报