// ---- Class for shared integer with lock --------------------------- public class SharedInt { // Instance variables. public int value; private boolean locked; // Constructor. public SharedInt(int initVal) { value = initVal; locked = false; } // Method to acquire lock. // Waits until lock is available. // Allows exceptions thrown by wait() to bubble up. public synchronized void lock() throws InterruptedException { while (locked) wait(); locked = true; } // Method to release lock. // Wakes up any waiting threads. public synchronized void unlock() { locked = false; notifyAll(); } }