CountDownLatch 使用场景
时间: 2023-10-17 18:37:53 浏览: 119
CountDownLatch的使用场景是,当某一线程在开始运行前需要等待其他n个线程执行完毕时。在这种情况下,可以将CountDownLatch的计数器初始化为n,然后每个任务线程执行完毕后调用countDown()方法减少计数器的值。当计数器的值变为0时,调用await()的线程就会被唤醒,然后可以继续执行后续操作。这在启动一个服务时主线程需要等待多个组件加载完毕之后再继续执行的场景中是非常典型的应用。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [每日一博 - CountDownLatch使用场景分析以及源码分析](https://blog.csdn.net/yangshangwei/article/details/121155013)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [CountDownLatch作用及使用场景](https://blog.csdn.net/arjun_yu/article/details/113522919)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文