AQS源码解读
创始人
2024-05-01 04:08:57
0

 retrantlock:

A、B、C3个线程,假设A线程lock()时候拿到了锁,state被A设置成了1。

  static final class NonfairSync extends Sync {private static final long serialVersionUID = 7316153563782823691L;/*** Performs lock.  Try immediate barge, backing up to normal* acquire on failure.*/final void lock() {if (compareAndSetState(0, 1))setExclusiveOwnerThread(Thread.currentThread());elseacquire(1);}protected final boolean tryAcquire(int acquires) {return nonfairTryAcquire(acquires);}}

 此时B和C调用lock时候就会走else,调用acquire(1);

我们可用看见这里的B和C会尝试获取锁,没有获取到就会把当前线程封装成node节点放到CLH双向队列里。让后阻断当前线程

AQS的 tryAcquire(),

实际是调用RetrantLock的非公平nonfairTryAcquire();

 

 此时 int c = getState();  c是1   并且current == getExclusiveOwnerThread()条件不成立。因为此时线程A占有锁,所以current此时是线程B 与 getExclusiveOwnerThread()是线程A,并不相等,返回false。

else if (current == getExclusiveOwnerThread()) {int nextc = c + acquires;if (nextc < 0) // overflowthrow new Error("Maximum lock count exceeded");setState(nextc);return true;
}

上面这段代码以上是假设B来了A刚好释放了锁,此时,突然A又来抢到了锁,这个时候current就是A线程和 getExclusiveOwnerThread()也是线程A,相等。又将state设置为1,并且占用锁。

将当前线程封装成node节点放到队列里

 private Node addWaiter(Node mode) {Node node = new Node(Thread.currentThread(), mode);// Try the fast path of enq; backup to full enq on failureNode pred = tail;if (pred != null) {node.prev = pred;if (compareAndSetTail(pred, node)) {pred.next = node;return node;}}enq(node);return node;}

你会发现底层调用了unsafe的接口 ,头指针主要是用于入队。

new Node()作为头节点 (第一个节点)

 

 

acquireQueued()干了一件什么事呢?

private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {int ws = pred.waitStatus;if (ws == Node.SIGNAL)/** This node has already set status asking a release* to signal it, so it can safely park.*/return true;if (ws > 0) {/** Predecessor was cancelled. Skip over predecessors and* indicate retry.*/do {node.prev = pred = pred.prev;} while (pred.waitStatus > 0);pred.next = node;} else {/** waitStatus must be 0 or PROPAGATE.  Indicate that we* need a signal, but don't park yet.  Caller will need to* retry to make sure it cannot acquire before parking.*/compareAndSetWaitStatus(pred, ws, Node.SIGNAL);}return false;}

 

没有抢到锁就阻塞,LockSupport.park(this);线程挂起不会继续往下执行

 

compareAndSetWaitStatus(pred, ws, Node.SIGNAL);

 unlock()f释放锁;

 

 public final boolean release(int arg) {if (tryRelease(arg)) {Node h = head;if (h != null && h.waitStatus != 0)unparkSuccessor(h);return true;}return false;}

 

释放锁实现:

 int c = getState() - releases; state的值此时是1,releases值也是1  所以c就变成了0,就释放了锁

 setExclusiveOwnerThread(null);设置当前占用线程为null,就是没有线程占用。

 setState(c);将state状态重新设置为0.

 

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...