Based on Chapter 4 of [Hai19]
(Usage hints for this presentation)
Computer Structures and Operating Systems 2022
Dr. Jens Lechtenbörger (License Information)

lock() when method is entered
lock() and
executes a methodunlock() when method is left
cwait(x): Blocks calling thread until csignal(x)
csignal(x): Starts at most one thread waiting for x
synchronized activates locks
this object during execution of method
E.g., for sample code from [Hai19] (for which you found races previously):
public synchronized void sell() { if (seatsRemaining > 0) { dispenseTicket(); seatsRemaining = seatsRemaining - 1; } else displaySorrySoldOut(); }
synchronized, thissell() from previous slides invoked on some object, say theater
theater has its own attribute seatsRemainingseatsRemaining is really this.seatsRemaining,
which is the same as theater.seatsRemaining
theater is unknown, theater
is the this object, which is used implicitlysynchronized, races arise when two threads invoke
sell() on the same object theater
synchronized, only one of the threads obtains the lock
on theater, so races are preventedsynchronized, locks for objects are activated
synchronized methods, thread needs to acquire lock for this objectthis object (e.g.,
seatsRemaining) are not locked
synchronized methods)sell() to use the monitor concept, recompile, and run
again. Observe the expected outcome.(Nothing to submit here; maybe ask questions online.)
synchronized activates lockpublic synchronized methodAsCS(...) {…}synchronized (syncObj) {…}syncObjwait() and notify(), explained
later; ignore for now)synchronized Examplepublic synchronized void sell() { if (seatsRemaining > 0) { dispenseTicket(); seatsRemaining = seatsRemaining - 1; } else displaySorrySoldOut(); }
synchronized avoids races
this object before executing
methodwait() and hope for notify() by the latterwait() and notify() in Javawait(): thread unlocks and leaves monitor, enters wait set
notify()
notifyAll()
// Based on Fig. 4.17 of [Hai17] public synchronized void insert(Object o) throws InterruptedException // Called by producer thread { while(numOccupied == buffer.length) // block thread as buffer is full; // cooperation from consumer required to unblock wait(); buffer[(firstOccupied + numOccupied) % buffer.length] = o; numOccupied++; // in case any retrieves are waiting for data, wake/unblock them notifyAll(); }
(Part of SynchronizedBoundedBuffer.java)
SynchronizedBoundedBuffer as shared resourcesynchronized methods on that bounded buffer
insert() and retrieve()wait() on buffer if unable to continue
this object used implicitly as target of wait()notifyAll() on same buffersynchronized nor wait/notifysynchronized
wait() and notify()This document is part of an Open Educational Resource (OER) course on Operating Systems. Source code and source files are available on GitLab under free licenses.
Except where otherwise noted, the work “OS06: Monitors in Java”, © 2017-2022 Jens Lechtenbörger, is published under the Creative Commons license CC BY-SA 4.0.
In particular, trademark rights are not licensed under this license. Thus, rights concerning third party logos (e.g., on the title slide) and other (trade-) marks (e.g., “Creative Commons” itself) remain with their respective holders.