import java.util.concurrent.CountDownLatch; public class Main { public static void main(String[] args) { CountDownLatch latch = new CountDownLatch(5); CountMonitor monitor = new CountMonitor(); Runnable task = new MyTask(monitor, latch); for(int i = 0; i< 5;i++){ new Thread(task).start(); } // Thread worker1 = new Thread(task); // Thread worker2 = new Thread(task); // worker1.start(); // worker2.start(); try { latch.await(); } catch (InterruptedException e) { System.out.println(e); } System.out.println("All threads are finished"); System.out.println(monitor.getCounter()); } }