import java.util.concurrent.CountDownLatch; public class Hei implements Runnable { int id; static CountDownLatch forste = new CountDownLatch(8); static CountDownLatch andre = new CountDownLatch(8); static CountDownLatch tredje = new CountDownLatch(8); Hei(int id) { this.id = id; } public void run() { try { System.out.println("Hei 1 fra " + id); forste.countDown(); forste.await(); System.out.println("Hei 2 fra " + id); andre.countDown(); andre.await(); System.out.println("Hei 3 fra " + id); tredje.countDown(); } catch(InterruptedException e) { System.exit(1); } } public static void main(String[] args) { for(int i = 0; i < 8; i++) { new Thread(new Hei(i)).start(); } try { tredje.await(); } catch(InterruptedException e) { System.exit(1); } System.out.println("Alle tradene har sagt hei tre ganger!"); } }