关键信息
state(volatile修饰)/双向链表数据结构
AQS有那些实现?
ReentrantLock、Semaphore、CountDownLatch
AQS分为同步队列和条件队列
public class BoundedBuffer<T> {
同步队列如何实现的?
是通过ConditionObject实现的,里面个双向队列
private final Lock lock = new ReentrantLock();
private final Condition notFull = lock.newCondition();
private final Condition notEmpty = lock.newCondition();
private final Object[] items = new Object[10];
private int putpos = 0;
private int takepos = 0;
private int count = 0;
public void put(T x) throws InterruptedException {
lock.lock();
try {
while (count == items.length) {
notFull.await(); // 等待缓冲区不满
}
items[putpos] = x;
if (++putpos == items.length) putpos = 0;
++count;
notEmpty.signal(); // 通知等待的消费者
} finally {
lock.unlock();
}
}
public T take() throws InterruptedException {
lock.lock();
try {
while (count == 0) {
notEmpty.await(); // 等待缓冲区不为空
}
T x = (T) items[takepos];
items[takepos] = null;
if (++takepos == items.length) takepos = 0;
--count;
notFull.signal(); // 通知等待的生产者
return x;
} finally {
lock.unlock();
}
}
public class ConditionObject implements Condition, java.io.Serializable {
private static final long serialVersionUID = 1173984872572414699L;
/** First node of condition queue. */
private transient Node firstWaiter;
/** Last node of condition queue. */
private transient Node lastWaiter;
public final void await() throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
//将当前线程加入等待队列
Node node = addConditionWaiter();
//释放当前的锁
long savedState = fullyRelease(node);
int interruptMode = 0;
//不在同步队列,则继续等待
while (!isOnSyncQueue(node)) {
LockSupport.park(this);
if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
break;
}
//将线程加入到等待队列
if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
interruptMode = REINTERRUPT;
if (node.nextWaiter != null) // clean up if cancelled
unlinkCancelledWaiters();
if (interruptMode != 0)
reportInterruptAfterWait(interruptMode);
}
public final void signal() {
if (!isHeldExclusively())
throw new IllegalMonitorStateException();
//获取队列首个节点
Node first = firstWaiter;
if (first != null)
//唤醒队首线程
doSignal(first);
}
标签:node,AQS,队列,lock,private,items,解析,final From: https://www.cnblogs.com/jichenghui/p/18361521