CountDownLatch latch = new CountDownLatch(NUMBER_OF_WORKERS); for (int i = 0; i < NUMBER_OF_WORKERS; i++) { new WorkerThread(latch).start(); } try { latch.await(); } catch (InterruptedException e) { // handle the exception } // Proceed with the boss thread
CountDownLatch latch = new CountDownLatch(TOTAL_TASKS); Executor executor = Executors.newFixedThreadPool(NUMBER_OF_THREADS); for (int i = 0; i < TOTAL_TASKS; i++) { executor.execute(new Task(i, latch)); } try { latch.await(); } catch (InterruptedException e) { // handle the exception } // Proceed with the main code logicPackage Library: The CountDownLatch class is part of the java.util.concurrent package in the Java standard library.