CountDownLatch - 理解await和countDown

时间:2012-03-23 03:02:23

标签: java concurrency java.util.concurrent

据Javadoc说: 使用给定计数初始化CountDownLatch。等待方法阻塞,直到当前计数达到零。

这意味着在下面的代码中,因为我将CountDownLatch初始化为1.一旦锁存器调用倒计时,所有线程都应该从其await方法中解除阻塞。

但是,主线程正在等待所有线程完成。而且,我没有加入主线程来结束其他线程。为什么主线程在等待?

    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.atomic.AtomicLong;

public class Sample implements Runnable {

    private CountDownLatch latch;

    public Sample(CountDownLatch latch)
    {
        this.latch = latch;
    }
    private static AtomicLong number = new AtomicLong(0);

    public long next() {
         return number.getAndIncrement();
    }

    public static void main(String[] args) {

        CountDownLatch latch = new CountDownLatch(1);
        for (int threadNo = 0; threadNo < 4000; threadNo++) {
          Runnable t = new Sample(latch);
          new Thread(t).start();
        }
        try {
            latch.countDown();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        try {
            latch.await();
            Thread.sleep(100);
            System.out.println("Count:"+next());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

2 个答案:

答案 0 :(得分:5)

尝试运行以下修改后的代码版本:

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicLong;

public class Test implements Runnable {

    private CountDownLatch latch;

    public Test(CountDownLatch latch)
    {
        this.latch = latch;
    }
    private static AtomicLong number = new AtomicLong(0);

    public long next() {
         return number.getAndIncrement();
    }

    public static void main(String[] args) {

        CountDownLatch latch = new CountDownLatch(1);
        for (int threadNo = 0; threadNo < 1000; threadNo++) {
          Runnable t = new Test(latch);
          new Thread(t).start();
        }
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println( "done" );
    }

    @Override
    public void run() {
        try {
            Thread.sleep(1000 + (int) ( Math.random() * 3000 ));
            System.out.println(next());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            latch.countDown();
        }
    }

}

您应该看到类似的内容:

  

0
完成
1
2
3
4
5
6
7
8 < br /> 9
  10
11
12
13
14
15
16
17
18

这表明主线程确实在第一个调用latch.await()

的线程后从latch.countDown()调用中解锁

答案 1 :(得分:1)

你正在启动4000个线程,它们只等待100毫秒。你最有可能压倒这个盒子(所有的线程都会在大致相同的时间结束)。在你的线程开始看起来添加一个睡眠并尝试增加超时以使它看起来像你期望的那样。