import java.util.concurrent.CountDownLatch; class CLD { static int[] ints = new int[3]; static class Task implements Runnable { CountDownLatch cdl; int idx; Task(CountDownLatch cdl, int idx){ this.cdl = cdl; this.idx = idx; } @Override public void run() { int var = 0; for (int i = 0; i < 1000; i++) { try { var++; Thread.sleep(1); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("Ferdig"); ints[idx-1] = idx*100; cdl.countDown(); System.out.println("Fortsetter"); } } public static void main(String[] args) throws InterruptedException { int threads = 3; CountDownLatch latch = new CountDownLatch(threads); for (int i = 0; i < threads; i++) { new Thread(new Task(latch, i+1)).start(); } System.out.println("Venter på trådene"); latch.await(); for (int i : ints) { System.out.println(i); } System.out.println("Alle tråder er ferdige."); } }