玩玩并发编程(六)--JUC--AQS

1. AQS是什么

AQS作为JUC包的核心框架,是 java.util.concurrent.locks.AbstractQueuedSynchronizer 准确的说就是一个阻塞队列,JUC所有原子类都依赖这个框架。重中之重!

2. AQS核心

To enqueue into a CLH lock, you atomically splice it in as new tail.
 To dequeue, you just set the head field.
            +------+  prev +-----+       +-----+
       head |      | <---- |     | <---- |     |  tail
            +------+       +-----+       +-----+
//链表形式的阻塞队列
static final class Node
//unsfae cas 操作
private static final Unsafe unsafe = Unsafe.getUnsafe();
unsafe.compareAndSwapObject

// 状态标记信号
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 int waitStatus;

3. 小结

AQS原理是任务队列的入栈出栈,使用unsafe cas操作来标记队列元素是否可执行,如果可执行,则移出队列同时执行改任务,同时考虑可重入和可中断效果,大量运用自旋和cas操作来做的锁,思路就是这样,代码不好看懂。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你是真的皮005

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值