zoukankan      html  css  js  c++  java
  • 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接口

  • 相关阅读:
    单例模式的三种写法
    ASP.NET如何下载大文件
    字符串是引用类型
    SQL 事务隔离级别
    Sql Server 锁
    设非主键为聚集索引
    C#如何使用SqlCacheDependency
    简易系统后台架构
    matlab cross 3*1 向量叉乘
    Matlab求齐次方程的解
  • 原文地址:https://www.cnblogs.com/zhouj-happy/p/6483817.html
Copyright © 2011-2022 走看看