import java.util.concurrent.CyclicBarrier;
public class CyclicBarrierEksempel {
public static void main(String[] args) {
int antTrader = 8;
CyclicBarrier barriere = new CyclicBarrier(antTrader);
for (int i = 0; i < antTrader; i++) {
new Thread(new Task(barriere)).start();
}
}
}
class Task implements Runnable {
private CyclicBarrier barriere;
Task(CyclicBarrier b) {
this.barriere = b;
}
public void run() {
System.out.println(Thread.currentThread().getName() + "gjor noe forste gang");
try {
barriere.await();
} catch (Exception e) {System.out.println(e);}
System.out.println(Thread.currentThread().getName() + "gjor noe for andre gang!");
try {
barriere.await();
} catch (Exception e) {System.out.println(e);}
System.out.println(Thread.currentThread().getName() + "gjor noe for tredje gang!");
}
}