import java.util.concurrent.*; class Problem { // felles data og metoder A static CyclicBarrier b; public static void main(String [] args) { Problem p = new Problem(); int num = Integer.parseInt(args[0]); int threadCount = Integer.parseInt(args[1]); if ((threadCount < 2) || (num < 2)) { System.out.println("Barrier count: " + num); System.out.println("Thread count: " + threadCount); System.exit(0); }; b = new CyclicBarrier(num); p.utfoer(threadCount); System.out.println(" Main TERMINATED"); } // end main void utfoer (int antT) { Thread [] t = new Thread [antT]; for (int i =0; i< antT; i++) ( t[i] = new Thread(new Arbeider(i))).start(); try { for (int i =0; i< antT; i++) t[i].join(); } catch(Exception e) {} } // end utfoer class Arbeider implements Runnable { // lokale data og metoder B int ind; void sync() { try{ b.await(); } catch (Exception e) { return;} } public Arbeider (int in) {ind = in;}; public void run() { // kalles naar traaden er startet if (ind == 1) try { TimeUnit.SECONDS.sleep(10); } catch (Exception e) { return;}; System.out.println("T"+ind+": start"); if (ind > 1) try { System.out.println("T"+ind+": Sleeping a little"); TimeUnit.SECONDS.sleep(1+ind); } catch (Exception e) { return;}; System.out.println("T"+ind+": trying to pass first time"); sync(); System.out.println("T"+ind+": Past first time"); if (ind > 1) try { System.out.println("T"+ind+": Sleeping a little"); TimeUnit.SECONDS.sleep(1+ind); } catch (Exception e) { return;}; System.out.println("T"+ind+": trying to pass second time"); sync(); System.out.println("T"+ind+": Past second time"); System.out.println("T"+ind+": end"); } // end run } // end indre klasse Arbeider } // end class Problem