The method declarations for bothpublic class CubbyHole { private int contents; private boolean available = false; public synchronized int get(int who) { ... } public synchronized void put(int who, int value) { ... } }
put
and
get
contain the
synchronized
keyword.
Whenever control enters a synchronized method, the thread that called the method locks the object whose method has been called. Other threads cannot call a synchronized method on the same object until the object is unlocked.