继承

public abstract class AbstractQueuedSynchronizerextends AbstractOwnableSynchronizerimplements java.io.Serializable {

AbstractOwnableSynchronizer设置独占线程

public abstract class AbstractOwnableSynchronizerimplements java.io.Serializable {/*** The current owner of exclusive mode synchronization.*/private transient Thread exclusiveOwnerThread;/*** Sets the thread that currently owns exclusive access.* A {@code null} argument indicates that no thread owns access.* This method does not otherwise impose any synchronization or* {@code volatile} field accesses.* @param thread the owner thread*/protected final void setExclusiveOwnerThread(Thread thread) {exclusiveOwnerThread = thread;}/*** Returns the thread last set by {@code setExclusiveOwnerThread},* or {@code null} if never set.  This method does not otherwise* impose any synchronization or {@code volatile} field accesses.* @return the owner thread*/protected final Thread getExclusiveOwnerThread() {return exclusiveOwnerThread;}}

Node节点,双向链表,
CountDownLatch,Semaphore,ReentrantReadWriteLock的读锁三个都使用共享模式的节点,因为state可以被多个线程修改
ReentrantLock,ReentrantReadWriteLock的写锁使用独占模式的节点,因为state可以被一个线程修改

    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*/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 enqueuing, 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();elsereturn p;}Node() {    // Used to establish initial head or SHARED marker}Node(Thread thread, Node mode) {     // Used by addWaiterthis.nextWaiter = mode;this.thread = thread;}Node(Thread thread, int waitStatus) { // Used by Conditionthis.waitStatus = waitStatus;this.thread = thread;}}

字段,Node节点组成的同步链表,头指针和尾指针,state同步状态值
ReentrantLock中,state指锁的重入次数
ReentrantReadWriteLock中,state的高16位指读锁的个数,低16位指写锁的重入次数
Semaphore中,指剩余凭证的个数
CountDownLatch中,指剩余线程的个数

/*** Head of the wait queue, lazily initialized.  Except for* initialization, it is modified only via method setHead.  Note:* If head exists, its waitStatus is guaranteed not to be* CANCELLED.*/private transient volatile Node head;/*** Tail of the wait queue, lazily initialized.  Modified only via* method enq to add new wait node.*/private transient volatile Node tail;/*** The synchronization state.*/private volatile int state;

AbstractQueuedSynchronizer源码相关推荐

  1. 条件队列java_Java并发系列(4)AbstractQueuedSynchronizer源码分析之条件队列

    AbstractQueuedSynchronizer内部维护了一个同步状态和两个排队区,这两个排队区分别是同步队列和条件队列. 我们还是拿公共厕所做比喻,同步队列是主要的排队区,如果公共厕所没开放,所 ...

  2. 面试官系统精讲Java源码及大厂真题 - 31 AbstractQueuedSynchronizer 源码解析(下)

    31 AbstractQueuedSynchronizer 源码解析(下) 低头要有勇气,抬头要有底气. 引导语 AQS 的内容太多,所以我们分成了两个章节,没有看过 AQS 上半章节的同学可以回首看 ...

  3. 面试官系统精讲Java源码及大厂真题 - 30 AbstractQueuedSynchronizer 源码解析(上)

    30 AbstractQueuedSynchronizer 源码解析(上) 不想当将军的士兵,不是好士兵. 引导语 AbstractQueuedSynchronizer 中文翻译叫做同步器,简称 AQ ...

  4. AbstractQueuedSynchronizer 源码分析(共享锁)

    为什么80%的码农都做不了架构师?>>>    源码看之前的问题 race condition如何避免? 工作流程是怎么样的? 使用什么方式实现的? 使用到的其他类说明和资料 Loc ...

  5. 【java】java JUC 同步器框架 AQS AbstractQueuedSynchronizer源码图文分析

    1.概述 转载:JUC锁: 锁核心类AQS详解 AQS是一个用来构建锁和同步器的框架,使用AQS能简单且高效地构造出应用广泛的大量的同步器,比如我们提到的ReentrantLock,Semaphore ...

  6. java conditionobject_Java AbstractQueuedSynchronizer源码阅读4-ConditionObject

    AbstractQueuedSynchronizer为锁机制维护了一个队列,需要获取锁的线程们排在队列中,只有排在队首的线程才有资格获取锁. ConditionObject是AbstractQueue ...

  7. AbstractQueuedSynchronizer源码深度解析

    #总体介绍 基于队列的抽象同步器,它是jdk中所有显示的线程同步工具的基础,像ReentrantLock/DelayQueue/CountdownLatch等等,都是借助AQS实现的.Java中已经有 ...

  8. 多线程5一AbstractQueuedSynchronizer源码分析一

    AQS的源码分析 <一> 文章目录 前言 1.什么是CAS ? 2.同步器类结构 3.CLH同步队列 4.AQS中静态内部类Node 5.方法分析 5.1.acquire(int arg ...

  9. AbstractQueuedSynchronizer 源码分析

    概述 Java的内置锁一直都是备受争议的,在JDK 1.6之前,synchronized这个重量级锁其性能一直都是较为低下,虽然在1.6后,进行大量的锁优化策略,但是与Lock相比synchroniz ...

  10. 并发编程专题五-AbstractQueuedSynchronizer源码分析

    PS:外号鸽子王不是白来的,鸽了好几天,也是因为比较忙,时间太少了,这篇东西有点多,需要慢慢消化.不知不觉居然写了4个多小时.... 一.什么是AQS aqs是AbstractQueuedSynchr ...

最新文章

  1. 2022-2028年中国塑料编织品的制造行业市场竞争态势及投资方向分析报告
  2. python渐变颜色表_python – 具有固定颜色渐变的np.histogram2D
  3. 黑马在线教育项目---5、使用填充器创建数据库数据
  4. python 爬虫 scrapy 和 requsts 哪个快_Python爬虫:Scrapy研读之Request/Reponse
  5. Redis操作List类型
  6. kubernetes--配置文件
  7. 【CodeForces - 264A】Escape from Stones (模拟,卡精度的处理)
  8. linux80端口检查,Linux下基于端口的服务检查脚本
  9. 【OpenCV 例程200篇】62. 图像锐化——钝化掩蔽
  10. 怎样在Swift中使用NSError
  11. 整个电脑键盘被锁住了_轻巧便携:罗技Pebble鹅卵石轻薄鼠标+K380蓝牙键盘上手体验...
  12. 土地土壤数据下载网站整理
  13. 蓝牙方案,蓝牙国密读卡器,TypeA/TypeB/Felca卡读写,分享蓝牙NFC读写器带USB接口,银行卡/CPU卡/NTAG213/Mifare卡蓝牙读写器,usb多通道通讯
  14. android手机屏幕同步电脑,Android手机如何将屏幕投射到计算机上?
  15. Au入门系列之九:多轨混音
  16. 如何查找计算机主机地址,ip地址查询 怎么查询电脑IP地址?
  17. 360全景倒车影像怎么看_360全景影像和倒车影像有什么区别
  18. 通过路由器来设置局域网下无线打印机打印
  19. linux 用谷歌浏览器总是崩溃,谷歌浏览器显示喔唷崩溃啦(谷歌浏览器经常崩溃怎么办)...
  20. GRUB4DOS 直接引导XP.ISO安装

热门文章

  1. 计算机网络学习笔记(2. 什么是网络协议)
  2. 报错:mysqldump‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
  3. 耳机的L和R是什么意思?
  4. 什么是Ultrabook
  5. USB3.1与Type-C有什么区别
  6. 都说“先卖人,后卖货”,或者说要想卖货,先卖人
  7. 小事也能看出一个人的能力
  8. hash算法在日常活动中的应用
  9. 区块链优秀github开源项目
  10. 听力技巧-真题代练及填空题