除了内置锁(synchronized)外,java AQS(AbstractQueuedSynchronizer)同步器几乎是所有同步容器,同步工具类的基础。ReentrantLock、ReentrantReadWriteLock就是通过内部类继承并实现AQS的接口来实现相关功能的。

  查看AQS的源码,包含以下几个内容:

  • Node数据结构定义,用于存放被阻塞(自旋或者挂起)的线程信息,或者存放等待condition的线程,通过标记区别,共用统一数据结构
  • node队列通用操作,包括队列操作和release操作(释放资源),node队列扩展于CLH队列,
  • 各种acquire操作(获取资源)的通用方法
  • 暴露的方法,包括tryAcquire/tryRelease等非阻塞接口方法,以及acquire/release等阻塞方法
  • 队列检查方法,包括队列中是否还有node、线程是否在队列中等
  • Condition相关数据结构以及方法
  • node节点字段以及同步器字段field的CAS辅助方法,用于实现原子CAS操作

  Node节点的数据结构如下所示:

    static final class Node {
/** Marker to indicate a node is waiting in shared mode */
// 非独占
static final Node SHARED = new Node();
/** Marker to indicate a node is waiting in exclusive mode */
// 独占
static final Node EXCLUSIVE = null; /** waitStatus value to indicate thread has cancelled */
// 线程已取消
static final int CANCELLED = 1;
/** waitStatus value to indicate successor's thread needs unparking */
// 后续节点线程需要调度
static final int SIGNAL = -1;
/** waitStatus value to indicate thread is waiting on condition */
// 线程等待某一条件
static final int CONDITION = -2;
/**
* waitStatus value to indicate the next acquireShared should
* unconditionally propagate
*/
// 下次acquireShared无条件传递
static final int PROPAGATE = -3; /**
* Status field, taking on only the values:
* SIGNAL: The successor of this node is (or will soon be)
* blocked (via park), so the current node must
* unpark its successor when it releases or
* cancels. To avoid races, acquire methods must
* first indicate they need a signal,
* then retry the atomic acquire, and then,
* on failure, block.
* CANCELLED: This node is cancelled due to timeout or interrupt.
* Nodes never leave this state. In particular,
* a thread with cancelled node never again blocks.
* CONDITION: This node is currently on a condition queue.
* It will not be used as a sync queue node
* until transferred, at which time the status
* will be set to 0. (Use of this value here has
* nothing to do with the other uses of the
* field, but simplifies mechanics.)
* PROPAGATE: A releaseShared should be propagated to other
* nodes. This is set (for head node only) in
* doReleaseShared to ensure propagation
* continues, even if other operations have
* since intervened.
* 0: None of the above
*
* The values are arranged numerically to simplify use.
* Non-negative values mean that a node doesn't need to
* signal. So, most code doesn't need to check for particular
* values, just for sign.
*
* The field is initialized to 0 for normal sync nodes, and
* CONDITION for condition nodes. It is modified using CAS
* (or when possible, unconditional volatile writes).
*/
volatile int waitStatus; /**
* Link to predecessor node that current node/thread relies on
* for checking waitStatus. Assigned during enqueing, and nulled
* out (for sake of GC) only upon dequeuing. Also, upon
* cancellation of a predecessor, we short-circuit while
* finding a non-cancelled one, which will always exist
* because the head node is never cancelled: A node becomes
* head only as a result of successful acquire. A
* cancelled thread never succeeds in acquiring, and a thread only
* cancels itself, not any other node.
*/
volatile Node prev; /**
* Link to the successor node that the current node/thread
* unparks upon release. Assigned during enqueuing, adjusted
* when bypassing cancelled predecessors, and nulled out (for
* sake of GC) when dequeued. The enq operation does not
* assign next field of a predecessor until after attachment,
* so seeing a null next field does not necessarily mean that
* node is at end of queue. However, if a next field appears
* to be null, we can scan prev's from the tail to
* double-check. The next field of cancelled nodes is set to
* point to the node itself instead of null, to make life
* easier for isOnSyncQueue.
*/
volatile Node next; /**
* The thread that enqueued this node. Initialized on
* construction and nulled out after use.
*/
volatile Thread thread; /**
* Link to next node waiting on condition, or the special
* value SHARED. Because condition queues are accessed only
* when holding in exclusive mode, we just need a simple
* linked queue to hold nodes while they are waiting on
* conditions. They are then transferred to the queue to
* re-acquire. And because conditions can only be exclusive,
* we save a field by using special value to indicate shared
* mode.
*/
Node nextWaiter; /**
* Returns true if node is waiting in shared mode
*/
final boolean isShared() {
return nextWaiter == SHARED;
} /**
* Returns previous node, or throws NullPointerException if null.
* Use when predecessor cannot be null. The null check could
* be elided, but is present to help the VM.
*
* @return the predecessor of this node
*/
final Node predecessor() throws NullPointerException {
Node p = prev;
if (p == null)
throw new NullPointerException();
else
return p;
} Node() { // Used to establish initial head or SHARED marker
} Node(Thread thread, Node mode) { // Used by addWaiter
this.nextWaiter = mode;
this.thread = thread;
} Node(Thread thread, int waitStatus) { // Used by Condition
this.waitStatus = waitStatus;
this.thread = thread;
}
}

java AQS(AbstractQueuedSynchronizer)同步器详解的更多相关文章

  1. java并发编程 | 锁详解:AQS,Lock,ReentrantLock,ReentrantReadWriteLock

    原文:java并发编程 | 锁详解:AQS,Lock,ReentrantLock,ReentrantReadWriteLock 锁 锁是用来控制多个线程访问共享资源的方式,java中可以使用synch ...

  2. Java 8 Stream API详解--转

    原文地址:http://blog.csdn.net/chszs/article/details/47038607 Java 8 Stream API详解 一.Stream API介绍 Java8引入了 ...

  3. java反射机制深入详解

    java反射机制深入详解  转自:http://www.cnblogs.com/hxsyl/archive/2013/03/23/2977593.html 一.概念 反射就是把Java的各种成分映射成 ...

  4. 国际化,java.util.ResourceBundle使用详解

    java.util.ResourceBundle使用详解   一.认识国际化资源文件   这个类提供软件国际化的捷径.通过此类,可以使您所编写的程序可以:          轻松地本地化或翻译成不同的 ...

  5. java之StringBuffer类详解

    StringBuffer 线程安全的可变字符序列. StringBuffer源码分析(JDK1.6): public final class StringBuffer extends Abstract ...

  6. java.util.ResourceBundle使用详解

    java.util.ResourceBundle使用详解   一.认识国际化资源文件   这个类提供软件国际化的捷径.通过此类,可以使您所编写的程序可以:          轻松地本地化或翻译成不同的 ...

  7. java之AbstractStringBuilder类详解

    目录 AbstractStringBuilder类 字段 构造器 方法   public abstract String toString() 扩充容量 void  expandCapacity(in ...

  8. java之StringBuilder类详解

    StringBuilder 非线程安全的可变字符序列 .该类被设计用作StringBuffer的一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍).如果可能,建议优先采用该类,因为在 ...

  9. java.util.ResourceBundle使用详解(转)

    java.util.ResourceBundle使用详解   一.认识国际化资源文件   这个类提供软件国际化的捷径.通过此类,可以使您所编写的程序可以:          轻松地本地化或翻译成不同的 ...

  10. java web.xml配置详解(转)

    源出处:java web.xml配置详解 1.常规配置:每一个站的WEB-INF下都有一个web.xml的设定文件,它提供了我们站台的配置设定. web.xml定义: .站台的名称和说明 .针对环境参 ...

随机推荐

  1. JS宽高理解

    1.clentWidth和clientHeight ①加入无padding.无滚动条显示占据位置 clientWidth=style.width ②假如有padding.无滚动 clientWidth ...

  2. shell编程(六)之数组

    数组: 存储多个元素的连续的内存空间 索引: 编号从0开始,属于数值索引 注意:索引也可支持使用自定义的格式,而不仅仅是数值格式 声明数组: declare -a ARRAY_NAME declare ...

  3. Linux (麒麟)系统 重启后无法登陆进图形界面

    登录图形化界面的时候,会显示GNOME电源管理器没启动等提示信息,会一直卡在登录界面 在启动的时候按ESC或者在登录界面crtl+alt +f3 进入字符终端界面 查看物理存储空间占用信息,可能会有一 ...

  4. Python中4位1进制数与float浮点数互相转换

    import struct s = 'F4CEF042' print(s) #<是小端,>是大端,f代表浮点数 print(struct.unpack('<f', bytes.fro ...

  5. git 执行pull错误如何撤销

    比如你当前所在的空间是trunk,但是执行了git pull origin branches,这时需要回滚回去,可以用一下步骤: 1.运行git reflog命令查看你的历史变更记录,如下: 69fd ...

  6. winform SerialPort串口通信问题

    一.串口通信简介串行接口(串口)是一种可以将接受来自CPU的并行数据字符转换为连续的串行数据流发送出去,同时可将接受的串行数据流转换为并行的数据字符供给CPU的器件.一般完成这种功能的电路,我们称为串 ...

  7. 【编程语言】Kotlin之object关键字

    在一个体重秤项目里面使用Kotlin开发,考虑到项目比较小型轻量,所以和团队申请决定使用Kotlin开发,以此熟悉和尝试一下Kotlin. 首先使用Kotlin之后,发现能和Java很好的兼容一起,开 ...

  8. [Database] 不知道表名和字段查找值=1234的数据.

      --如果表比较大,时间会比较长 DECLARE @searchValue NVARCHAR(50) SET @searchValue='1234' DECLARE @t TABLE ( rowNu ...

  9. ajax得到后端数据一直提示为[object Object]解决方法

    前段ajax <script type="text/javascript"> function requestJson() { $.ajax({ type : &quo ...

  10. SpringBoot使用日志

    1.日志框架 日志门面 日志实现 JCL.SLF4J.jboss-logging Log4j.JUL.Log4j2.Logback 日志门面:SLF4J 日志实现:Logback SpringBoot ...