import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.Condition; import java.util.ArrayList; public class Romskip { private ArrayList varer = new ArrayList<>(); private final int KAPASITET = 5; private Lock l?s = new ReentrantLock(); private Condition ikkeFull = l?s.newCondition(); private Condition ikkeTom = l?s.newCondition(); public void leggInn(String vare) throws InterruptedException { l?s.lock(); try { while (varer.size() >= KAPASITET) { System.out.println("Romskipet er fullt. Leveringskip venter ..."); ikkeFull.await(); } varer.add(vare); System.out.println("Leveringsskipet leverte: " + vare); ikkeTom.signalAll(); } finally { l?s.unlock(); } } public String hentUt() throws InterruptedException { l?s.lock(); try { while (varer.isEmpty()) { System.out.println("Romskipet er tomt. Astronaut venter ..."); ikkeTom.await(); } String vare = varer.remove(varer.size() - 1); System.out.println("Astronauten hentet: " + vare); ikkeFull.signalAll(); return vare; } finally { l?s.unlock(); } } }