根据 Javadoc: CountDownLatch 使用给定计数进行初始化。 wait 方法会阻塞,直到当前计数达到零。
这意味着在下面的代码中,因为我将 CountDownLatch 初始化为 1。一旦闩锁调用倒计时,所有线程都应该从其等待方法中解除阻塞。
但是,主线程正在等待所有线程完成。而且,我没有将主线程加入到其他线程的末尾。为什么主线程在等待?
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();
}
}
}
尝试运行以下代码的修改版本:
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
9
10
11
12
13
14
15
16
17
18
19
这表明主线程实际上在第一个线程调用
latch.await()
之后从
latch.countDown()
调用中解除了阻塞
您正在启动 4000 个线程,它们只等待 100 毫秒。您很可能会压倒该框(并且所有线程将大致在同一时间结束)。在线程开始外观中添加睡眠并尝试增加超时以查看其按您的预期工作。
这是一个很好的例子供参考。无论依赖线程需要多长时间才能准备好(通过调用await()),主线程都会暂停并等待它们准备好。
import lombok.SneakyThrows;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CountDownLatchDemo {
@SneakyThrows
public static void main(String[] args) {
CountDownLatch cl = new CountDownLatch(4);
ExecutorService es = Executors.newFixedThreadPool(4);
DependentService ds1 = new DependentService(cl, "DependenService 1");
DependentService ds2 = new DependentService(cl, "DependenService 2");
DependentService ds3 = new DependentService(cl, "DependenService 3");
DependentService ds4 = new DependentService(cl, "DependenService 4");
es.submit(ds1);
es.submit(ds2);
es.submit(ds3);
es.submit(ds4);
Thread.sleep((long) (Math.random() * 100));
System.out.println("Main Thread ready");
es.shutdownNow();
}
private static class DependentService implements Runnable {
private final String name;
private final CountDownLatch cl;
public DependentService(CountDownLatch cl, String name) {
this.cl = cl;
this.name = name;
}
@SneakyThrows
@Override
public void run() {
System.out.println(name + "working");
cl.await();
System.out.println(name + " Initialised!");
}
}
}
在输出中“主线程已准备好”将始终是最后一行。