import java.util.concurrent.*; class HovedprogramCyclicBarrier{ public static void main(String[] args) { CyclicBarrier barriere = new CyclicBarrier(2, new DoSomething()); for (int i = 0; i < 4 ; i++){ Teller teller = new Teller(barriere, i); Thread traad = new Thread(teller); traad.start(); } } } class Teller implements Runnable { CyclicBarrier barriere; int nummer; public Teller(CyclicBarrier barriere, int nummer){ this.barriere = barriere; this.nummer = nummer; } public void run(){ System.out.println("Tr?d ("+nummer+") starter opp!"); try { Thread.sleep( (long)(Math.random() * 4000 + 2000)); System.out.println("Tr?d ("+nummer+") venter"); barriere.await(); System.out.println("Tr?d ("+nummer+") fortsetter"); } catch(InterruptedException e){} catch(BrokenBarrierException e){} System.out.println("Tr?d ("+nummer+") er ferdig!"); } } class DoSomething implements Runnable { public void run(){ System.out.println("TO TR?DER HAR KALT P? AWAIT()"); } }