import java.util.concurrent.locks.*; public class SamlebaandMonitor { /* * En monitor som låser tilgangen til en * teller (papirflyLagd). */ Lock laas = new ReentrantLock(); Condition ikkeFullt = laas.newCondition(); int papirflyLagd; int maxPapirfly; public SamlebaandMonitor(int samlebaandStoerrelse) { maxPapirfly = samlebaandStoerrelse; } public void settInn() throws InterruptedException { laas.lock(); try { while (papirflyLagd == maxPapirfly) { /* * Her venter en tråd på at det * skal bli plass til flere fly */ ikkeFullt.await(); } papirflyLagd++; } finally { laas.unlock(); } } public void hentUt() { laas.lock(); try { papirflyLagd--; ikkeFullt.signalAll(); } finally { laas.unlock(); } } }