import java.util.concurrent.*; import java.util.concurrent.locks.*; class HovedprogramUke12{ public static void main(String[] args) { int arrayStrl = 0; int tallPrTraad = 0; try { arrayStrl = Integer.parseInt(args[0]); tallPrTraad = Integer.parseInt(args[1]); } catch(Exception e){ System.out.println("Noe gikk galt! husk strl p? array som argument."); System.exit(-1); } if (arrayStrl % tallPrTraad != 0){ System.out.println("Send med et tall som g?r opp i 100!"); System.exit(-1); } System.out.println("\nOppretter array! (strl: "+arrayStrl+" tall per tr?d: "+tallPrTraad+")"); int[] liste = new int[arrayStrl]; for (int i = 0; i < arrayStrl; i++){ liste[i] = (int) (Math.random() * 1000000); //returnere tall mellom 0 og 1 000 000; } System.out.println("ferdig med ? lage array!"); int antallTraader = arrayStrl / tallPrTraad; //oppretter en CountDownLatch (barriere) og monitoren v?r. CountDownLatch barriere = new CountDownLatch(antallTraader); MinMonitor monitor = new MinMonitor(); //starter tr?den som skal finne det aller minste tallet FinnMinsteTallHele oppgave = new FinnMinsteTallHele(monitor, barriere); Thread traad = new Thread(oppgave); traad.start(); //starter alle tr?dene som finner det minste tallet i en delmengde av arrayet. for (int i = 0; i < antallTraader; i++){ FinnMinsteTallDel oppgave2 = new FinnMinsteTallDel(liste, i*tallPrTraad, ((i+1)*tallPrTraad), monitor, barriere); Thread traad2 = new Thread(oppgave2); traad2.start(); } System.out.println("Alle tr?der er startet!"); } } class FinnMinsteTallDel implements Runnable{ private int[] liste; private int start; private int slutt; private int minsteTall; private MinMonitor monitor; private CountDownLatch barriere; public FinnMinsteTallDel(int[] liste, int start, int slutt, MinMonitor monitor, CountDownLatch barriere){ this.liste = liste; this.start = start; this.slutt = slutt; this.monitor = monitor; this.barriere = barriere; minsteTall = liste[start]; } public void run(){ for (int i = start; i < slutt; i++){ if (liste[i] < minsteTall){ minsteTall = liste[i]; } } monitor.settMinsteTall(minsteTall); barriere.countDown(); } } class FinnMinsteTallHele implements Runnable{ MinMonitor monitor; CountDownLatch barriere; public FinnMinsteTallHele(MinMonitor monitor, CountDownLatch barriere){ this.monitor = monitor; this.barriere = barriere; } public void run(){ System.out.println("HENTER MINSTE TALL!"); //long start = System.currentTimeMillis(); try{ barriere.await(); } catch(InterruptedException e){ System.out.println("noe gikk galt!"); } int minsteTall = monitor.hentMinsteTall(); System.out.println("FIKK MINSTE TALL: " + minsteTall); //long totalTid = System.currentTimeMillis() - start; //System.out.println("TOTAL TID = " + totalTid/1000F + " sekunder!"); } } class MinMonitor{ int minsteTall; Lock laas = new ReentrantLock(); public MinMonitor(){ minsteTall = 10000001; } public void settMinsteTall(int tall){ laas.lock(); try{ if (tall < minsteTall){ minsteTall = tall; } } finally{ laas.unlock(); } } public int hentMinsteTall(){ return minsteTall; } }