Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the uses of CountDownLatch

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "What are the uses of CountDownLatch". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "What are the uses of CountDownLatch"!

CountDownLatch is a synchronization utility class used to coordinate synchronization between multiple threads, or to communicate between threads (rather than acting as a mutual exclusion).

CountDownLatch enables a thread to wait for other threads to finish their work before resuming execution. This is done using a counter. The initial value of the counter is the number of threads. When each thread completes its task, the counter is decremented by one. When the counter value is 0, it indicates that all threads have completed some tasks, and then the thread waiting on CountDownLatch can resume executing the next task.

Use of CountDownLatch

CountDownLatch Typical Usage: 1. A thread waits for n threads to finish executing before starting to run. Initialize the CountDownLatch counter to new CountDownLatch(n), decrement the counter by 1 countdownLatch.countDown() every time a task thread finishes executing, and wake up the thread awaiting () on CountDownLatch when the counter value becomes 0. A typical application scenario is when starting a service, the main thread needs to wait for multiple components to load before continuing execution.

Typical usage of CountDownLatch: 2, to achieve the maximum parallelism of multiple threads to start executing tasks. Note that parallelism, not concurrency, emphasizes that multiple threads start executing at the same time. Similar to a race, put multiple threads at the start, wait for the starting gun to sound, and then start running at the same time. This is done by initializing a shared CountDownLatch(1), initializing its calculator to 1, multiple threads first countdownlatch.await() before starting a task, and when the main thread calls countDown(), the counter goes to 0, and multiple threads wake up simultaneously.

Take a chestnut:

package com.example.demo.CountDownLatchDemo;import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * */public class CountdownLatchTest {public static void main(String[] args) { ExecutorService service = Executors.newFixedThreadPool(3);final CountDownLatch latch = new CountDownLatch(3);for (int i = 0; i < 3; i++) { Runnable runnable = new Runnable() { @Overridepublic void run() {try { System.out.println("Child Thread" + Thread.currentThread().getName() + "Start Execution"); Thread.sleep((long) (Math.random() * 10000)); System.out.println("Child Thread"+Thread.currentThread().getName()+"Execution Completed"); catch.countDown();//If the current thread calls this method, the count is decremented by one} catch (InterruptedException e) { e.printStackTrace(); } } }; service.execute(runnable); }try { System.out.println("Main Thread"+Thread.currentThread().getName()+"Wait for child thread execution to complete... "); latch.await();//Block the current thread until the counter value is 0System.out.println("Main Thread"+Thread.currentThread().getName()+"Start execution... "); } catch (InterruptedException e) { e.printStackTrace(); } }} At this point, I believe that everyone has a deeper understanding of "CountDownLatch", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report