import java.util.concurrent.CountDownLatch; import java.util.concurrent.locks.*; import java.util.Random; class HovedprogramBarrierer { public static void main(String[] args) { MonitorStorstTall monitor = new MonitorStorstTall(); int antallTraader = 10; CountDownLatch count = new CountDownLatch(antallTraader); for (int i = 0; i < antallTraader; i++){ Arbeider a = new Arbeider(monitor, count); Thread t = new Thread(a); t.start(); } try { count.await(); } catch(InterruptedException e){ System.out.println(e); } System.out.println("Storste tall er: " + monitor.hentTall()); } } class MonitorStorstTall { int storstTall = 0; Lock laas = new ReentrantLock(); public void giTall(int tall){ laas.lock(); try { if (tall > storstTall){ storstTall = tall; } } finally { laas.unlock(); } } public int hentTall(){ return storstTall; } } class Arbeider implements Runnable { MonitorStorstTall monitor; CountDownLatch count; public Arbeider(MonitorStorstTall monitor, CountDownLatch count){ this.monitor = monitor; this.count = count; } public void run(){ Random r = new Random(); int tall = r.nextInt(100); try { Thread.sleep(tall * 15); } catch(InterruptedException e){ System.out.println(e); } monitor.giTall(tall); count.countDown(); System.out.println("Traad la til tallet: " + tall); } }