AQS实现共享锁

目录

共享锁

CountDownLatch

作用:

构造函数

内部类Sync

countDown

releaseShared(1)

tryReleaseShared(arg)

doReleaseShared()

await

tryAcquireShared

doAcquireSharedInterruptibly

CycliBarrier

作用:

属性:

构造器:

打破栅栏

开启下一代

await

Semaphore

作用:

构造函数

请求资源:

释放资源:


共享锁

上面介绍的都是独占锁,下面介绍共享锁

CountDownLatch

作用:

将大任务分成各个子任务 主线程等待所有子任务执行完毕;最后一个子任务线程负责唤醒所有在阻塞队列中的主线程

构造函数

//唯一的一个构造函数  

//看过CountDownLatch内部类Sync 的构造函数 就会知道 count值直接设置到了锁状态上

public CountDownLatch(int count) {

    if (count < 0) throw new IllegalArgumentException("count < 0");

    this.sync = new Sync(count);

}

内部类Sync

//还是 继承自 AbstractQueuedSynchronizer

private static final class Sync extends AbstractQueuedSynchronizer {

    private static final long serialVersionUID = 4982264981922014374L;

 

    Sync(int count) {

        setState(count);

    }

 

    int getCount() {

        return getState();

    }

 

    protected int tryAcquireShared(int acquires) {

        return (getState() == 0) ? 1 : -1;

    }

 

    protected boolean tryReleaseShared(int releases) {

        // Decrement count; signal when transition to zero

        for (;;) {

            int c = getState();

            if (c == 0)

                return false;

            int nextc = c-1;

            if (compareAndSetState(c, nextc))

                return nextc == 0;

        }

    }

}

countDown

 

public void countDown() {

    sync.releaseShared(1);

}

releaseShared(1)

位置:AbstractQueuedSynchronizer 1340
方法目的:将共享锁的 锁状态 减去 arg值

方法流程:

public final boolean releaseShared(int arg) {

if (tryReleaseShared(arg)) {

    //如果走到这里说明 锁状态 为 0   要去唤醒在阻塞队列中的线程了

        doReleaseShared();

        return true;

    }

    return false;

}

tryReleaseShared(arg)

位置:CountDownLatch 176
方法目的: 

方法流程:自旋将锁状态 减去 releases 值  

返回锁状态值 是否等于 0

protected boolean tryReleaseShared(int releases) {

    // Decrement count; signal when transition to zero

    for (;;) {

        int c = getState();

        if (c == 0)

            return false;

        int nextc = c-1;

        if (compareAndSetState(c, nextc))

            return nextc == 0;

    }

}

doReleaseShared()

位置:AbstractQueuedSynchronizer 670
方法目的: 

方法流程:

 

这里抛出一个疑问,上面独占锁的时候,把head的后继节点唤醒之后,后继变成了head 那么是怎么唤醒新head的后继节点呢? 其实就是每次unlock的时候

唤醒 head的后继    那么这里呢共享锁怎么唤醒后继呢?递归

private void doReleaseShared() {

    /*

     * Ensure that a release propagates, even if there are other

     * in-progress acquires/releases.  This proceeds in the usual

     * way of trying to unparkSuccessor of head if it needs

     * signal. But if it does not, status is set to PROPAGATE to

     * ensure that upon release, propagation continues.

     * Additionally, we must loop in case a new node is added

     * while we are doing this. Also, unlike other uses of

     * unparkSuccessor, we need to know if CAS to reset status

     * fails, if so rechecking.

     */

    for (;;) {

        Node h = head;

        if (h != null && h != tail) {

            int ws = h.waitStatus;

            if (ws == Node.SIGNAL) {

                if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0))

                    continue;            // loop to recheck cases

                unparkSuccessor(h);

            }

            else if (ws == 0 &&

                     !compareAndSetWaitStatus(h, 0, Node.PROPAGATE))

                continue;                // loop on failed CAS

        }

        //如果 h是头结点了

        if (h == head)                   // loop if head changed

            break;

    }

}

await

位置:CountDownLatch 230
方法目的: 如果共享锁状态 不为0 也就是 子任务没有执行完 那么线程需要挂起  唤醒之后 递归唤醒所有阻塞队列中的线程

方法流程:

public void await() throws InterruptedException {

    sync.acquireSharedInterruptibly(1);

}

 

位置:AbstractQueuedSynchronizer 1299
方法目的: 

方法流程:

  1. 如果共享锁完全释放完毕 那么线程不需要等待 子任务完成
  2. 如果没有完全释放 线程挂起
  3. 线程唤醒之后  递归将所有的阻塞队列中的线程唤醒

public final void acquireSharedInterruptibly(int arg)

        throws InterruptedException {

    if (Thread.interrupted())

        throw new InterruptedException();

    if (tryAcquireShared(arg) < 0)

        doAcquireSharedInterruptibly(arg);

}

tryAcquireShared

位置:CountDownLatch 172
方法目的:判断共享锁是否完全释放

方法流程:

protected int tryAcquireShared(int acquires) {

    return (getState() == 0) ? 1 : -1;

}

doAcquireSharedInterruptibly

位置:AbstractQueuedSynchronizer 980
方法目的: 

方法流程:

private void doAcquireSharedInterruptibly(int arg)

    throws InterruptedException {

    //将 节点加入到阻塞队列  和独占锁的进入阻塞队列一样 只是这里标识为共享模式

    final Node node = addWaiter(Node.SHARED);

    boolean failed = true;

    try {

        for (;;) {

            final Node p = node.predecessor();

            if (p == head) {

                int r = tryAcquireShared(arg);

                if (r >= 0) {

                    //这一步和独占锁有所不一样 设置新头结点的同时 递归释放 后继

                    setHeadAndPropagate(node, r);

                    p.next = null; // help GC

                    failed = false;

                    return;

                }

            }

            if (shouldParkAfterFailedAcquire(p, node) &&

                parkAndCheckInterrupt())

                throw new InterruptedException();

        }

    } finally {

        if (failed)

            cancelAcquire(node);

    }

}

 

位置:AbstractQueuedSynchronizer 708
方法目的:设置头结点并 唤醒后继节点

方法流程:

private void setHeadAndPropagate(Node node, int propagate) {

    Node h = head; // Record old head for check below

    setHead(node);

    /*

     * Try to signal next queued node if:

     *   Propagation was indicated by caller,

     *     or was recorded (as h.waitStatus either before

     *     or after setHead) by a previous operation

     *     (note: this uses sign-check of waitStatus because

     *      PROPAGATE status may transition to SIGNAL.)

     * and

     *   The next node is waiting in shared mode,

     *     or we don't know, because it appears null

     *

     * The conservatism in both of these checks may cause

     * unnecessary wake-ups, but only when there are multiple

     * racing acquires/releases, so most need signals now or soon

     * anyway.

     */

    if (propagate > 0 || h == null || h.waitStatus < 0 ||

        (h = head) == null || h.waitStatus < 0) {

        Node s = node.next;

        if (s == null || s.isShared())

            doReleaseShared();

    }

}

 

CycliBarrier

作用:

可重复使用的栅栏,上面CountDownLatch只能使用一次,是基于AQS共享模式写的。

CycliBarrier是可以重复使用 是利用Condition写的

属性:

 

public class CyclicBarrier {

    /**

     * Each use of the barrier is represented as a generation instance.

     * The generation changes whenever the barrier is tripped, or

     * is reset. There can be many generations associated with threads

     * using the barrier - due to the non-deterministic way the lock

     * may be allocated to waiting threads - but only one of these

     * can be active at a time (the one to which {@code count} applies)

     * and all the rest are either broken or tripped.

     * There need not be an active generation if there has been a break

     * but no subsequent reset.

     */

    //栅栏是可以重复使用的   这里可以叫做代 或者 周期

    private static class Generation {

        boolean broken = false;

    }

 

    /** The lock for guarding barrier entry */

    private final ReentrantLock lock = new ReentrantLock();

    //基于Condition  也就是条件

    private final Condition trip = lock.newCondition();

    //参与线程数

    private final int parties;

    //线程都到栅栏后  需要执行的任务

    private final Runnable barrierCommand;

    //当前这一代

    private Generation generation = new Generation();

 

    /**

     * Number of parties still waiting. Counts down from parties to 0

     * on each generation.  It is reset to parties on each new

     * generation or when broken.

     */

     //还没有到达栅栏的线程数

    //到达栅栏的线程数 = parties - count

    private int count;

 

构造器:

//第一个参数指定栅栏 需要等几个线程都到栅栏后 才能继续执行

//第二个参数是指定 线程都到栅栏后需要执行的任务

public CyclicBarrier(int parties, Runnable barrierAction) {

    if (parties <= 0) throw new IllegalArgumentException();

    this.parties = parties;

    this.count = parties;

    this.barrierCommand = barrierAction;

}

//需要等几个线程都到栅栏后 才能继续执行

public CyclicBarrier(int parties) {

    this(parties, null);

}

打破栅栏

 
private void breakBarrier() {
    //栅栏打破状态 设置为 true
generation.broken = true;
//重置count 
count = parties;
//唤醒栅栏中所有线程(条件队列中的线程)
    trip.signalAll();
}

 

开启下一代

 
private void nextGeneration() {
// signal completion of last generation
//唤醒所有的线程
    trip.signalAll();
// set up next generation
//初始化
    count = parties;
    generation = new Generation();
}

 

await

位置:CyclicBarrier 360
方法目的: 

方法流程:

public int await() throws InterruptedException, BrokenBarrierException {
    try {
        return dowait(false, 0L);
    } catch (TimeoutException toe) {
        throw new Error(toe); // cannot happen
    }
}

 

位置:CyclicBarrier 198
方法目的: 

方法流程:

private int dowait(boolean timed, long nanos)
    throws InterruptedException, BrokenBarrierException,
           TimeoutException {
final ReentrantLock lock = this.lock;
//加锁
    lock.lock();
    try {
        final Generation g = generation;
        //如果栅栏”破”了 抛出异常
        if (g.broken)
            throw new BrokenBarrierException();
        //如果线程中断了 打破 栅栏  
        if (Thread.interrupted()) {
            breakBarrier();
            throw new InterruptedException();
        }
 
        int index = --count;
        //如果 线程都到达了栅栏 那么 如果有需要执行的任务 就执行
        开启下一代 返回0
        if (index == 0) {  // tripped
            boolean ranAction = false;
            try {
                final Runnable command = barrierCommand;
                if (command != null)
                    command.run();
                ranAction = true;
                nextGeneration();
                return 0;
            } finally {
                if (!ranAction)
                    breakBarrier();
            }
        }
 
        // loop until tripped, broken, interrupted, or timed out
        //这里就是那些没有 达到栅栏 但是 仍有未到达栅栏的线程的处理
        for (;;) {
            try {
               //处理未带超时时间
                if (!timed)
                    trip.await();
               //带超时时间
                else if (nanos > 0L)
                    nanos = trip.awaitNanos(nanos);
            } catch (InterruptedException ie) {
               //到这里说明是 condition.await时候中断了
                if (g == generation && ! g.broken) {
                    breakBarrier();
                    throw ie;
                } else {
                   //这里是说明 开启了 下一代 或者 已经打破了栅栏
                    // We're about to finish waiting even if we had not
                    // been interrupted, so this interrupt is deemed to
                    // "belong" to subsequent execution.
                    Thread.currentThread().interrupt();
                }
            }
 
            if (g.broken)
                throw new BrokenBarrierException();
            //正常情况下 当最后一个线程到达栅栏后 开启下一代 直接返回0
            //如果condition的await等待过程中出现异常后 抛出            
            //如果 condition的await超时了 那么是不会抛出异常的 就会到下面的if语句中
            if (g != generation)
                return index;
            //如果超时了 打破栅栏  抛出超时异常
            if (timed && nanos <= 0L) {
                breakBarrier();
                throw new TimeoutException();
            }
        }
    } finally {
        lock.unlock();
    }
}

 

Semaphore

作用:

Semaphore是AQS的共享锁实现, semaphore可以理解为是一个池,每个线程在执行之前都需要从池子里面拿到相应的资源才能运行。公平与非公平的区别就是在 请求资源的时候检查一下阻塞队列中是否有等待的

构造函数

 
public Semaphore(int permits) {
    sync = new NonfairSync(permits);
}
//第一个参数是 池子里面的资源数     fair是用公平/非公平策略
public Semaphore(int permits, boolean fair) {
    sync = fair ? new FairSync(permits) : new NonfairSync(permits);
}

请求资源:

acquire***

以其中一个 acuire()方法为例

 
public void acquire() throws InterruptedException {
    sync.acquireSharedInterruptibly(1);
}
public final void acquireSharedInterruptibly(int arg)
        throws InterruptedException {
    if (Thread.interrupted())
        throw new InterruptedException();
    if (tryAcquireShared(arg) < 0)
        doAcquireSharedInterruptibly(arg);
}
protected int tryAcquireShared(int acquires) {
    return nonfairTryAcquireShared(acquires);
}
 

 

//怎么请求资源的
final int nonfairTryAcquireShared(int acquires) {
    for (;;) {
        int available = getState();
        //将 锁状态 减去 请求资源数
        int remaining = available - acquires;
        if (remaining < 0 ||
            compareAndSetState(available, remaining))
            return remaining;
    }
}

 

//如果请求资源失败 那么就进入阻塞队列
private void doAcquireSharedInterruptibly(int arg)

    throws InterruptedException {

    final Node node = addWaiter(Node.SHARED);

    boolean failed = true;

    try {

        for (;;) {

            final Node p = node.predecessor();

            if (p == head) {

                int r = tryAcquireShared(arg);

                if (r >= 0) {

                    setHeadAndPropagate(node, r);

                    p.next = null; // help GC

                    failed = false;

                    return;

                }

            }

            if (shouldParkAfterFailedAcquire(p, node) &&

                parkAndCheckInterrupt())

                throw new InterruptedException();

        }

    } finally {

        if (failed)

            cancelAcquire(node);

    }

}

 

释放资源:

release() 释放一个资源  release(int permits)

 
public void release(int permits) {

    if (permits < 0) throw new IllegalArgumentException();

    sync.releaseShared(permits);

}

 

//看到方法名称就知道是老套路
public final boolean releaseShared(int arg) {

    if (tryReleaseShared(arg)) {

        doReleaseShared();

        return true;

    }

    return false;

}

 

//自旋将锁状态 更新
protected final boolean tryReleaseShared(int releases) {

    for (;;) {

        int current = getState();

        int next = current + releases;

        if (next < current) // overflow

            throw new Error("Maximum permit count exceeded");

        if (compareAndSetState(current, next))

            return true;

    }

}

 

//唤醒所有阻塞队列中等待的线程
private void doReleaseShared() {

    /*

     * Ensure that a release propagates, even if there are other

     * in-progress acquires/releases.  This proceeds in the usual

     * way of trying to unparkSuccessor of head if it needs

     * signal. But if it does not, status is set to PROPAGATE to

     * ensure that upon release, propagation continues.

     * Additionally, we must loop in case a new node is added

     * while we are doing this. Also, unlike other uses of

     * unparkSuccessor, we need to know if CAS to reset status

     * fails, if so rechecking.

     */

    for (;;) {

        Node h = head;

        if (h != null && h != tail) {

            int ws = h.waitStatus;

            if (ws == Node.SIGNAL) {

                if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0))

                    continue;            // loop to recheck cases

                unparkSuccessor(h);

            }

            else if (ws == 0 &&

                     !compareAndSetWaitStatus(h, 0, Node.PROPAGATE))

                continue;                // loop on failed CAS

        }

        if (h == head)                   // loop if head changed

            break;

    }

}

个人微信公众号程序员探索之路,公众号内点击网赚获取彩蛋,大家一起加油

你可能感兴趣的:(多线程)