/* Taken from https://java2blog.com/java-cyclicbarrier-example/ (accessed 24.08.2022) and slightly modified. */ import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; public class Student implements Runnable{ private final CyclicBarrier cyclicBarrier; private long sleepTime; // time that the student sleeps public Student(CyclicBarrier cyclicBarrier,long sleepTime){ this.cyclicBarrier = cyclicBarrier; this.sleepTime = sleepTime; } @Override public void run() { try { Thread.sleep(sleepTime); // student sleeps for some time System.out.println(Thread.currentThread().getName() + " is waiting for "+(cyclicBarrier.getParties()-cyclicBarrier.getNumberWaiting()-1)+ " other students to arrive"); /* * The student waits for other students to also call their await method. * As soon as enough students have called their await method, the party can start. */ cyclicBarrier.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } System.out.println("As "+cyclicBarrier.getParties()+ " students have arrived, " + Thread.currentThread().getName() + " starts to party now!"); } }