Count Down Latch explanation

 

Very important paragraph on how the CountDownLatch works. CountDownLatch is a flexible latch implementation that can be used in any of these situations;

it allows one or more threads to wait for a set of events to occur. The latch state consists of a counter initialized

to a positive number, representing the number of events to wait for. The countDown method decrements the counter,

indicating that an event has occurred, and the await methods wait for the counter to reach zero, which happens

when all the events have occurred. If the counter is nonzero on entry, await blocks until the counter reaches zero,

the waiting thread is interrupted, or the wait times out.

The following 2 paragraphs are very important on explaining an example how we use CountDownLatch TestHarness illustrates two common uses for latches. TestHarness creates a number of threads

that run a given task concurrently. It uses two latches, a "starting gate" and an "ending gate". The starting gate is

initialized with a count of one; the ending gate is initialized with a count equal to the number of worker threads.

The first thing each worker thread does is wait on the starting gate; this ensures that none of them starts working

until they all are ready to start. The last thing each does is count down on the ending gate; this allows the master

thread to wait efficiently until the last of the worker threads has finished, so it can calculate the elapsed time.

 

Why did we bother with the latches in TestHarness instead of just starting the threads immediately after they are created?

Presumably, we wanted to measure how long it takes to run a task n times concurrently. If we simply created and

started the threads, the threads started earlier would have a "head start" on the later threads, and the degree of

contention would vary over time as the number of active threads increased or decreased. Using a starting gate allows

the master thread to release all the worker threads at once, and the ending gate allows the master thread to wait for the

last thread to finish rather than waiting sequentially for each thread to finish.

 
 
1)    The main thread start many threads, and let all of them await the startGate
2)    The main thread countdown the startGate---release them all
3)    The main thread await the endGate
4)    Every thread ends, the thread will countdown the endGate
5)    When all the threads finished, the main thread will be released to the next 
code line—which calculate the long end, calculate the total execution time.
 
 
public class TestHarness {
    public long timeTasks(int nThreads, final Runnable task)
            throws InterruptedException {
        final CountDownLatch startGate = new CountDownLatch(1);
        final CountDownLatch endGate = new CountDownLatch(nThreads);
 
        for (int i = 0; i < nThreads; i++) {
            Thread t = new Thread() {
                public void run() {
                    try {
startGate.await();//await() means current thread holds on
                        try {
                            task.run();
                        } finally {
//if countdown() reaches 0, release all threads waiting on this gate
                            endGate.countDown();
                        }
                    } catch (InterruptedException ignored) { }
                }
            };
            t.start();
        }
 
        long start = System.nanoTime();
        startGate.countDown();
        endGate.await();
        long end = System.nanoTime();
        return end-start;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值