AQS解析

关键信息
state(volatile修饰)/双向链表数据结构
AQS有那些实现?
ReentrantLock、Semaphore、CountDownLatch 
AQS分为同步队列和条件队列
public class BoundedBuffer<T> {  
    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();  
        }  
    }  
 
 
同步队列如何实现的?
是通过ConditionObject实现的,里面个双向队列
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);
}

 

posted @   辉辉、  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示