CountDownLatch

CountDownLatch is quite similar to WaitGroup in go. It is used to wait for a set of operations to complete before proceeding.

import java.util.concurrent.CountDownLatch;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(3);
        
        Runnable task = () -> {
            System.out.println(Thread.currentThread().getName() + " is working.");
            latch.countDown();
        };
        
        for (int i = 0; i < 3; i++) {
            new Thread(task).start();
        }

        latch.await();
        System.out.println("All threads are done, proceeding with main thread.");
    }
}

Expected output:

Thread-1 is working.
Thread-0 is working.
Thread-2 is working.
All threads are done, proceeding with main thread.