Java / Java Multithreading
What is CountDownLatch in Java?
CountDownLatch is a synchronizer type which allows one Thread to wait for one or more Threads before starts processing.
A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.
A CountDownLatch is initialized with a given count. The await methods block until the current count reaches zero due to invocations of the countDown() method, after which all waiting threads are released and any subsequent invocations of await return immediately. This is not a reusable phenomenon, the count cannot be reset. If you need a version that resets the count, consider using a CyclicBarrier.
package CountDownLatchExample; import java.util.concurrent.CountDownLatch; public class CountDownLatchExample { static final CountDownLatch latch = new CountDownLatch(2); public static void main(String[] args) throws InterruptedException { MyThread thread = new MyThread(latch); new Thread(thread).start(); new Thread(thread).start(); latch.await(); System.out.println(Thread.currentThread().getName() + " done."); } } class MyThread implements Runnable { CountDownLatch latch; MyThread(CountDownLatch latch) { this.latch = latch; } @Override public void run() { System.out.println(Thread.currentThread().getName() + " completed."); latch.countDown(); } }
Dogecoin
! Earn free bitcoins up to $250 now by signing up.
Earn bitcoins upto $250 (free), invest in other Cryptocurrencies when you signup with blockfi.
Use the referral link: Signup now and earn!
Using BlockFi, don't just buy crypto - start earning on it. Open an interest account with up to 8.6% APY, trade currencies, or borrow money without selling your assets.
Join CoinBase
! We'll both receive $10 in free Bitcoin when they buy or sell their first $100 on Coinbase! Available in India also.
Use the referral Join coinbase!
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull
! Receive free stock by signing up using the link: Webull signup.
More Related questions...