package threadCooperation; import java.util.Random; public class ClassRoom { private boolean roomReady = false, computerReady = false; private Random random = new Random(123); private static int totalClasses = 0; public synchronized void fixRoom() { while (roomReady && computerReady) { System.out.println("No repairs needed..."); try { this.wait(); } catch(InterruptedException e) { e.printStackTrace(); } } roomReady = computerReady = true; System.out.println("FixRoom: Room Ready"); this.notify(); } public synchronized void startTeaching() { try { while (!roomReady || !computerReady) { this.notify(); System.out.println("Teacher waiting..."); this.wait(); } System.out.println("Teaching class: " + ++totalClasses); roomReady = random.nextBoolean(); computerReady = random.nextBoolean(); String status = "After Teaching: roomReady " + roomReady; status += " computerReady " + computerReady; System.out.println(status); } catch(InterruptedException e){e.printStackTrace();} } }