public static void main(String[] args) throws InterruptedException {
System.out.println("包工头 等待所有所有工人搬运砖头......");
int waitThreadCount=3;
CountDownLatch countDownLatch = new CountDownLatch(waitThreadCount);
Vector<String> list = new Vector<>();
for (int i = 0; i < waitThreadCount; i++) {
new Thread(()->{
System.out.println(Thread.currentThread().getName()+"开始执行搬运工作");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
list.add(Thread.currentThread().getName()+"搬运了3万块砖头");
System.out.println(Thread.currentThread().getName()+"搬运完了");
countDownLatch.countDown();
},"工人-"+i).start();
}
countDownLatch.await();
System.out.println("所有工人搬运完毕,包工头检查搬运结果");
System.out.println("清点结果:"+list);
}