绥芬河市建设局网站,wordpress会员小图标,硅橡胶东莞网站建设,wordpress 缩略图截图概念设计初衷#xff1a;该类利用 状态队列 实现了一个同步器#xff0c;更多的是提供一些模板方法#xff08;子类必须重写#xff0c;不然会抛错#xff09;。
设计功能#xff1a;独占、共享模式两个核心#xff0c;state、Queue2.1 statesetState、compareAndSetSta…概念设计初衷该类利用 状态队列 实现了一个同步器更多的是提供一些模板方法子类必须重写不然会抛错。
设计功能独占、共享模式两个核心state、Queue2.1 statesetState、compareAndSetState都是用于修改同步状态。看类名其实就知道一个是线程不安全的setState一个是使用了乐观锁来保证线程安全compareAndSetState。使用场景setState应用于释放资源的线程因为同一时间只有一个使用这个线程不安全的方法去修改state的值所以不会发生并发安全问题compareAndSetState应用于尝试获取同步器的资源由于同一时间可能存在多个资源竞争锁所以需要使用unsfte类的cas保证线程安全private volatile int state; // 同步状态值0空闲0有多少个线程在同步队列中等待
protected final int getState() { // 获取同步状态return state;
}
protected final void setState(int newState) { // 修改同步状态state newState;
}
protected final boolean compareAndSetState(int expect, int update) {return unsafe.compareAndSwapInt(this, stateOffset, expect, update); // 通过unsafe类的cas修改同步状态
}2.2 Queue底层带头、尾节点的双向链表private transient volatile Node head; // 头节点
private transient volatile Node tail; // 尾节点
static final class Node {volatile Node prev; // 前一个节点volatile Node next; // 后一个节点
}核心获取同步锁流程3.1 acquirepublic final void acquire(int arg) {// 尝试获取资源失败且成功加入同步队列则阻塞线程if (!tryAcquire(arg) acquireQueued(addWaiter(Node.EXCLUSIVE), arg)) selfInterrupt(); // 获取不到资源或者加入队列失败那就中断该线程
}3.2 tryAcquireprotected boolean tryAcquire(int arg) {throw new UnsupportedOperationException(); // 模板方法让子类实现
}3.3 addWaiterprivate Node addWaiter(Node mode) {Node node new Node(Thread.currentThread(), mode); // 转化为同步节点队列节点类型// 尾插法Node pred tail; // 指向尾节点if (pred ! null) {node.prev pred; // 新节点的前一个节点指向尾节点if (compareAndSetTail(pred, node)) { // 因为同时间内有多个线程进入队列所以使用cas置换尾节点pred.next node; // 原尾节点的下一个指针指向新插入的节点return node;}}enq(node); // 队列为空需要初始化队列插入return node;
}3.4 enqprivate Node enq(final Node node) {for (;;) { // 自旋创建到成功为止Node t tail;if (t null) { // 还是并发安全问题保守判断一下是不是有人抢先一步if (compareAndSetHead(new Node()))tail head;} else {node.prev t; // 插入节点指向尾巴if (compareAndSetTail(t, node)) { // 交换尾节点t.next node; // 上一个节点指向当前插入节点return t; }}}
}3.5 acquireQueuedfinal boolean acquireQueued(final Node node, int arg) {boolean failed true;try {boolean interrupted false;for (;;) {final Node p node.predecessor(); // 获取上一个节点if (p head tryAcquire(arg)) { // 检查一下上一个节点是不是头节点是的话尝试获取资源setHead(node); // 设置头节点为当前节点p.next null; // GC掉因为当前节点获取到资源说明上一个节点已经执行完毕业务了failed false; // 设置成功return interrupted; // 不阻塞}if (shouldParkAfterFailedAcquire(p, node) // 没有获取到资源把线程挂起别浪费资源parkAndCheckInterrupt())interrupted true;}} finally {if (failed)cancelAcquire(node); // 获取到资源取消尝试获取资源}
}3.6 shouldParkAfterFailedAcquireprivate static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {int ws pred.waitStatus; // 上一个节点的wait状态if (ws Node.SIGNAL) // 上一个节点是SIGNAL状态说明可以阻塞回去等通知就行return true;if (ws 0) { // 如果大于0说明是CANCELLED状态那就把前面那些废物节点扔掉do {node.prev pred pred.prev; } while (pred.waitStatus 0); // 扔啊扔扔到前一个节点不是废物节点pred.next node;} else {compareAndSetWaitStatus(pred, ws, Node.SIGNAL); // 小于0说明前面有节点更改成SIGNAL状态}return false;
}3.6.1 waitStatusstatic final int CANCELLED 1; // 废弃状态有些加入了又不想等就玩儿
static final int SIGNAL -1; // 等待激活下一个节点的状态下一个节点肯定是被Condiction.await()
static final int CONDITION -2; // 条件状态中间节点
static final int PROPAGATE -3; // 共享模式下的节点状态3.7 parkAndCheckInterruptprivate final boolean parkAndCheckInterrupt() {LockSupport.park(this); // 挂起当前线程return Thread.interrupted();
}3.7.1 interruptedpublic static boolean interrupted() {return currentThread().isInterrupted(true); // 获取当前线程是否为中断状态
}
private native boolean isInterrupted(boolean ClearInterrupted); // 清除中断标志获取同步锁流程图