JUC并发工具类--AQS

管程 — Java同步的设计思想

管程:指的是管理共享变量以及对共享变量的操作过程,让他们支持并发。
互斥:同一时刻只允许一个线程访问共享资源;
同步:线程之间如何通信、协作。

MESA模型

在管程的发展史上,先后出现过三种不同的管程模型,分别是Hasen模型、Hoare模型和MESA模型。现在正在广泛使用的是MESA模型
MEA管程模型

管程中引入了条件变量的概念,而且每个条件变量都对应有一个等待队列。条件变量和等待队列的作用是解决线程之间的同步问题。

Java中针对管程有两种实现:

  1. 一种是基于Object的Monitor机制,用于synchronized内置锁的实现
  2. 一种是抽象队列同步器AQS,用于JUC包下Lock锁机制的实现

AQS(AbstractQueuedSynchronizer:抽象队列同步器)

AQS简介

java.util.concurrent包中的大多数同步器实现都是围绕着共同的基础行为,比如等待队列、条件队列、独占获取、共享获取等,而这些行为的抽象就是基于AbstractQueuedSynchronizer(简称AQS)实现的,AQS是一个抽象同步框架,可以用来实现一个依赖状态的同步器。

JDK中提供的大多数的同步器如Lock, Latch, Barrier等,都是基于AQS框架来实现的。

  • 一般是通过一个内部类Sync继承 AQS
  • 将同步器所有调用都映射到Sync对应的方法

AQS的实现类
AQS具备的特性:

  • 阻塞等待队列
  • 共享/独占
  • 公平/非公平
  • 可重入
  • 允许中断

AQS核心结构

AQS内部维护属性state。

state表示资源的可用状态。

    /**
     * The synchronization state.
     */
    private volatile int state;

state三种访问方式
    /**
     * Returns the current value of synchronization state.
     * This operation has memory semantics of a {@code volatile} read.
     * @return current state value
     */
    protected final int getState() {
        return state;
    }

    /**
     * Sets the value of synchronization state.
     * This operation has memory semantics of a {@code volatile} write.
     * @param newState the new state value
     */
    protected final void setState(int newState) {
        state = newState;
    }

    /**
     * Atomically sets synchronization state to the given updated
     * value if the current state value equals the expected value.
     * This operation has memory semantics of a {@code volatile} read
     * and write.
     *
     * @param expect the expected value
     * @param update the new value
     * @return {@code true} if successful. False return indicates that the actual
     *         value was not equal to the expected value.
     */
    protected final boolean compareAndSetState(int expect, int update) {
        // See below for intrinsics setup to support this
        return unsafe.compareAndSwapInt(this, stateOffset, expect, update);
    }

两种资源访问方式

  • Exclusive-独占,只有一个线程能执行,如ReentrantLock
  • Share-共享,多个线程可以同时执行,如Semaphore/CountDownLatch

AQS实现时主要实现的方法

isHeldExclusively()
该线程是否正在独占资源。只有用到condition才需要去实现它。
tryAcquire(int)
独占方式。尝试获取资源,成功则返回true,失败则返回false。
tryRelease(int)
独占方式。尝试释放资源,成功则返回true,失败则返回false。
tryAcquireShared(int)
共享方式。尝试获取资源。负数表示失败;0表示成功,但没有剩余可用资源;正数表示成功,且有剩余资源。
tryReleaseShared(int)
共享方式。尝试释放资源,如果释放后允许唤醒后续等待结点返回true,否则返回false。

源码注释

AQS定义的两种队列

AQS 队列中5个节点状态(waitStatus):

  1. 值为0,初始化状态,表示当前节点在sync队列中,等待着获取锁。
  2. CANCELLED,值为1,表示当前的线程被取消;
  3. SIGNAL,值为-1,表示当前节点的后继节点包含的线程需要运行,也就是unpark;
  4. CONDITION,值为-2,表示当前节点在等待condition,也就是在condition队列中;
  5. PROPAGATE,值为-3,表示当前场景下后续的acquireShared能够得以执行;
	/** 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;

同步等待队列

用于维护获取锁失败时入队的线程。

AQS当中的同步等待队列也称CLH队列(Craig、Landin、Hagersten三人发明的),是一种基于双向链表数据结构的队列,是FIFO先进先出线程等待队列,Java中的CLH队列是原CLH队列的一个变种,线程由原自旋机制改为阻塞机制。

AQS 依赖CLH同步队列来完成同步状态的管理:

  • 当前线程如果获取同步状态失败时,AQS则会将当前线程已经等待状态等信息构造成一个节点(Node)并将其加入到CLH同步队列,同时会阻塞当前线程。
  • 当同步状态释放时,会把首节点唤醒(公平锁),使其再次尝试获取同步状态。
  • 通过signal或signalAll将条件队列中的节点转移到同步队列。(由条件队列转化为同步队列)
    同步等待队里示意图

条件等待队列

调用await()的时候会释放锁,然后线程会加入到条件队列,调用signal()唤醒的时候会把条件队列中的线程节点移动到同步队列中,等待再次获得锁。

AQS中条件队列是使用单向列表保存的,用nextWaiter来连接:

  • 调用await()方法阻塞线程;
  • 当前线程存在于同步队列的头结点,调用await()方法进行阻塞(从同步队列转化到条件队列)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值