CountDownLatch闭锁

用法

CountDownLatch是一种灵活的闭锁实现,它可以使一个活多个线程等待一组事件发生。闭锁状态包括一个计数器,该计数器被初始化一个正数,表示需要等待的时间数量。countDown方法递减计数器,表示有一个事件已经发生了,而await方法等待计数器达到0,这表示所有需要等待的时间都已经发生。如果计数器的值非零,那么await会一直阻塞知道计数器为零,或者等待中的线程中断,或者等待超时。

用一段代码来演示用法:

public class CountDownLatchTest {

    public static void main(String[] args) throws InterruptedException {
        final CountDownLatch countDownLatch = new CountDownLatch(3);
        new Thread() {
            @Override
            public void run() {
                System.out.println("thread one finish");
                countDownLatch.countDown();
            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                System.out.println("thread two finish");
                countDownLatch.countDown();
            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                System.out.println("thread three finish");
                countDownLatch.countDown();
            }
        }.start();

        countDownLatch.await();
        System.out.println("main thread finish");
    }
}

CountDownLatch初始值为3,当三个线程都countDown一线,每次countDown,闭锁中的state(初始值为3)都减为1,主线程调用了await方法,所以只有等其他三个线程都调用countDown方法才会唤醒。运行结果如下:

thread one finish
thread two finish
thread three finish
main thread finish

源码分析

countDount方法

CountDownLatch也用到了AQS,关于AQS可以看上一篇文章,这里不赘述。CountDownLatch中最重要的就是countDown方法和await方法,其中await方法有两个重载版本,一个是有等待时间限制的,先来看countDown方法:

public void countDown() {
        sync.releaseShared(1);
}

sync中的releaseShared(1)方法:

public final boolean releaseShared(int arg) {
        if (tryReleaseShared(arg)) {
            doReleaseShared();
            return true;
        }
        return false;
}

其中if判断作用是先去检查state值,在我们上一个程序中初始值为3,当state值大于0的话,countDown方法才合法,执行了doReleaseShared方法,其方法如下:

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;
        }
    }

其中waitStatus很重要,用来控制线程的阻塞/唤醒,有以下几种含义:

//代表线程已经被取消
static final int CANCELLED = 1;

//代表后续节点需要唤醒
static final int SIGNAL = -1;

//代表线程在condition queue中,等待某一条件
static final int CONDITION = -2;

//代表后续结点会传播唤醒的操作,共享模式下起作用
static final int PROPAGATE = -3;

doReleaseShared方法是一个死循环,先去检查调用await方法正在线程的状态,若为SIGNAL状态,说明,次线程正在等待,然后CAS操作(主要用于检查state是否为0),若state为0,CAS操作成功,线程唤醒,此时线程状态值state为0,通过循环去唤醒等待队列的等待线程,前提是state为0。

await方法

awati方法有两个重载版本,一个有等待时间限制,先看没有等待时间限制。

    public void await() throws InterruptedException {
        sync.acquireSharedInterruptibly(1);
    }

sync中的acquireSharedInterruptibly(1)方法:

    public final void acquireSharedInterruptibly(int arg)
            throws InterruptedException {
        if (Thread.interrupted())
            throw new InterruptedException();
        if (tryAcquireShared(arg) < 0)
            doAcquireSharedInterruptibly(arg);
    }

先去检查线程的状态,先通过tryAcquireShared(arg) < 0检查state状态,只有state大于0,await方法才有效

若state大于0,则执行doAcquireSharedInterruptibly(arg)方法:

    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);
        }
    }

一大串代码就是AQS中没有获取到锁加入等待队列的操作。

await方法有时间限制

大致和上面的没有await方法差不多,就是有一个等待时间限制,若在规定时间内state没有为0,那么就不会等待,我们可以改上面的演示代码来验证我们的猜想,修改好的程序如下:

public class CountDownLatchTest {

    public static void main(String[] args) throws InterruptedException {
        final CountDownLatch countDownLatch = new CountDownLatch(3);
        new Thread() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("thread one finish");
                countDownLatch.countDown();
            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                System.out.println("thread two finish");
                countDownLatch.countDown();
            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                System.out.println("thread three finish");
                countDownLatch.countDown();
            }
        }.start();

        countDownLatch.await(1000, TimeUnit.MILLISECONDS);
        System.out.println("main thread finish");
    }
}

让其中一个线程sleep3秒,然后主线程的等待时间为1秒,程序运行结果如下:

先是显示

thread two finish
thread three finish

然后一秒后主线程的等待时间已过,然后state还不为0,所以没有继续阻塞

结果:

main thread finish

再过去两秒,第一个线程执行完成了:

thread one finish

参考:

http://www.cnblogs.com/zhaoyanjun/p/5486726.html

http://blog.csdn.net/lmj623565791/article/details/26626391

http://www.cnblogs.com/zhanjindong/p/java-concurrent-package-aqs-AbstractQueuedSynchronizer.html

你可能感兴趣的:(java并发,java)