(Usage hints for this presentation)
IT Systems, Summer Term 2025
Dr. Jens Lechtenbörger (License Information)
Continue playing Deadlock Empire
Monitor in game is just a lock, which is locked with enter()
and unlocked with exit()lock() when method is entered
lock() and
executes a methodunlock() when method is left
Abstract data type with MX guarantee
Methods encapsulate local variables
Thread enters monitor via method
cwait(x): Blocks calling thread until csignal(x)
csignal(x): Starts at most one thread waiting for x
In Java, classes and objects come with built-in locks
Keyword synchronized activates locks
Automatic locking of this object during execution of method
E.g., for sample code from (Hailperin 2019) (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 prevented
With synchronized, locks for objects are activated
synchronized methods, thread needs to acquire lock for this objectMethods cannot be locked
Individual attributes of the this 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.)
MX based on monitor concept
Every Java object (and class) comes with
Monitor with lock (not activated by default)
synchronized activates lock
For method: public synchronized methodAsCS(...) {…}
this object upon call (Class
object for static methods)
Or for block: synchronized (syncObj) {…}
syncObj
Wait set (set of threads; wait() and notify(), explained
next)
Classical synchronization problems
Synchronization for data structure necessary
wait() and hope for notify() by the latterwait() and notify() in JavaWaiting via blocking
wait(): thread unlocks and leaves monitor, enters wait set
Notifications
notify()
notifyAll()
synchronized Java Method// 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)
synchronizedSynchronizedBoundedBuffer 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/notifySource files are available on GitLab (check out embedded submodules) under free licenses. Icons of custom controls are by @fontawesome, released under CC BY 4.0.
Except where otherwise noted, the work “MX in Java”, © 2017-2025 Jens Lechtenbörger, is published under the Creative Commons license CC BY-SA 4.0.