AQS
juc很多操作都是基于AQS(AbstractQueuedSynchronizer)
不同于自旋锁,juc使用的是volatile机制的状态变量;
内部也有一个阻塞线程的等待队列
每个线程都被封装在一个Node结点中
static final class Node {
// 当前线程被取消
static final int CANCELLED = 1;
// 结点被移除队列时唤醒后继节点
static final int SIGNAL = -1;
// 结点在条件队列里面
static final int CONDITION = -2;
/**
* waitStatus value to indicate the next acquireShared should
* unconditionally propagate
*/
static final int PROPAGATE = -3;
/** 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;
volatile int waitStatus;
volatile Node prev;
volatile Node next;
/**
* The thread that enqueued this node. Initialized on
* construction and nulled out after use.
*/
volatile Thread thread;
Node nextWaiter;
/**
* Returns true if node is waiting in shared mode
*/
final boolean isShared() {
return nextWaiter == SHARED;
}
final Node predecessor() throws NullPointerException {
Node p = prev;
if (p == null)
throw new NullPointerException();
else
return p;
}
Node() { // Used to establish initial head or SHARED marker
}
Node(Thread thread, Node mode) { // Used by addWaiter
this.nextWaiter = mode;
this.thread = thread;
}
Node(Thread thread, int waitStatus) { // Used by Condition
this.waitStatus = waitStatus;
this.thread = thread;
}
}
等待锁时线程封装进入队列尾部或者成为唯一节点,锁释放时,队列头部节点出对;
Lock
可重入锁的设计
内部继承AQS的Sync类来实现锁相关操作,线程可以重复获得锁,内部计数器在每次获得锁时计数器加1,计数器变为0时,锁释放;
锁获取实现:acquire()方法先尝试一次tryAcquire操作,如果失败,则调用acquireQueue()方法把当前线程加入到同步队列中去,这个时候可能会反复的阻塞与唤醒这个线程,直到后续的tryAcquire操作成功。
锁释放实现:将state值减1,然后判断锁是否被完全释放,如果被完全释放,则唤醒继任节点。
Condition
条件变量的实现,类似于OS中的信号量的实现,是实现BlockingQueue的基础,变量value>=0时表示有多少资源可以分配,当value<0时,表示欠缺多少资源,线程必须在等待队列中排队。
主要操作是await(),signal()
Signal()操作在资源可用时唤醒相应线程
Await()操作在资源不可用时线程必须等待
二者必须配对存在。
ReadWriteLock
读写锁的实现,读写锁分为读锁和写锁,读取数据必须获取读锁,可以存在多个读锁,但写锁必须只能存在一个,读锁写锁之间互斥。
Java读写锁在设计上采用volatile状态变量、CAS数据更新机制和条件变量结合的策略,获取读锁是,如果写锁被线程拥有,那么读者进入等待队列,否则可以获取锁,获取写锁时,如果读者仍持有锁,那么写者等待。