import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.Condition; import java.util.ArrayList; class Monitor{ private ArrayList alleSlekter; private ArrayList> alleSyke; private Lock laas; private Condition tom; private int antalLesFraFil; public Monitor(int antalLesFraFil){ this.antalLesFraFil = antalLesFraFil; alleSlekter = new ArrayList<>(); alleSyke = new ArrayList<>(); laas = new ReentrantLock(); tom = laas.newCondition(); } public void leggTilSlekt(Person person){ laas.lock(); try { alleSlekter.add(person); tom.signalAll(); antalLesFraFil--; } catch(Exception e) { System.out.println("Feil i leggTilSlekt"); }finally{ laas.unlock(); } } public Person hentSlekt(){ laas.lock(); try { while(antalLesFraFil != 0 && alleSlekter.isEmpty()){ System.out.println("Venter"); tom.await(); System.out.println("Ferdig emd ? vente"); } if(!alleSlekter.isEmpty()){ return alleSlekter.remove(0); } } catch(Exception e) { System.out.println("Feil i henSlekt"); e.printStackTrace(); }finally{ laas.unlock(); } return null; } public void leggTilSyke(ArrayList> alleSykeInn){ laas.lock(); try { System.out.println("Legger til!"); alleSyke.addAll(alleSykeInn); } catch(Exception e) { System.out.println("Feil i alleSykeINN"); }finally{ laas.unlock(); } } public ArrayList> hentAlleSyke(){ laas.lock(); try { return alleSyke; } catch(Exception e) { System.out.println("Feil i hent alle syke"); e.printStackTrace(); }finally{ laas.unlock(); } return null; } }