CSCI 5828 Foundations of Software Engineering Lecture 16 Monitors Condition Synchronization Slides created by Magee and Kramer for the Concurrency textbook 1 Magee Kramer 2nd Edition Chapter 5 Monitors Condition Synchronization Concurrency monitors condition synchronization 2 Magee Kramer 2nd Edition monitors condition synchronization Concepts monitors encapsulated data access procedures mutual exclusion condition synchronization single access procedure active in the monitor nested monitors Models guarded actions Practice private data and synchronized methods exclusion wait notify and notifyAll for condition synch single thread active in the monitor at a time Concurrency monitors condition synchronization 3 Magee Kramer 2nd Edition 5 1 Condition synchronization A controller is required for a carpark which only permits cars to enter when the carpark is not full and does not permit cars to leave when there are no cars in the carpark Car arrival and departure are simulated by separate threads Concurrency monitors condition synchronization 4 Magee Kramer 2nd Edition carpark model Events or actions of interest arrive and depart Identify processes arrivals departures and carpark control Define each process and interactions structure CARPARK ARRIVALS arrive CARPARK CONTROL Concurrency monitors condition synchronization depart DEPARTURES 5 Magee Kramer 2nd Edition carpark model CARPARKCONTROL N 4 SPACES N SPACES i 0 N when i 0 arrive SPACES i 1 when i N depart SPACES i 1 ARRIVALS arrive ARRIVALS DEPARTURES depart DEPARTURES CARPARK ARRIVALS CARPARKCONTROL 4 DEPARTURES Guarded actions are used to control arrive and depart LTS Concurrency monitors condition synchronization 6 Magee Kramer 2nd Edition carpark program Model all entities are processes interacting by actions Program need to identify threads and monitors thread active entity which initiates output actions monitor passive entity which responds to input actions For the carpark CARPARK ARRIVALS arrive CARPARK CONTROL Concurrency monitors condition synchronization depart DEPARTURES 7 Magee Kramer 2nd Edition carpark program class diagram Runnable Applet CarPark arrivals departures ThreadPanel Arrivals carpark carDisplay Departures CarParkControl arrive depart CarParkCanvas disp Concurrency monitors condition synchronization DisplayCarPark We have omitted DisplayThread and GraphicCanvas threads managed by ThreadPanel 8 Magee Kramer 2nd Edition carpark program Arrivals and Departures implement Runnable CarParkControl provides the control condition synchronization Instances of these are created by the start method of the CarPark applet public void start CarParkControl c new DisplayCarPark carDisplay Places arrivals start new Arrivals c departures start new Departures c Concurrency monitors condition synchronization 9 Magee Kramer 2nd Edition carpark program Arrivals and Departures threads class Arrivals implements Runnable CarParkControl carpark Arrivals CarParkControl c carpark c public void run try Similarly Departures while true which calls ThreadPanel rotate 330 carpark depart carpark arrive ThreadPanel rotate 30 catch InterruptedException e How do we implement the control of CarParkControl Concurrency monitors condition synchronization 10 Magee Kramer 2nd Edition Carpark program CarParkControl monitor class CarParkControl protected int spaces protected int capacity mutual exclusion by synch methods CarParkControl int n capacity spaces n condition synchronization synchronized void arrive spaces block if full spaces 0 synchronized void depart spaces block if empty spaces N Concurrency monitors condition synchronization 11 Magee Kramer 2nd Edition condition synchronization in Java Java provides a thread wait set per monitor actually per object with the following methods public final void notify Wakes up a single thread that is waiting on this object s wait set public final void notifyAll Wakes up all threads that are waiting on this object s wait set public final void wait throws InterruptedException Waits to be notified by another thread The waiting thread releases the synchronization lock associated with the monitor When notified the thread must wait to reacquire the monitor before resuming execution Concurrency monitors condition synchronization 12 Magee Kramer 2nd Edition condition synchronization in Java We refer to a thread entering a monitor when it acquires the mutual exclusion lock associated with the monitor and exiting the monitor when it releases the lock Wait causes the thread to exit the monitor permitting other threads to enter the monitor Monitor Thread A Thread B data wait notify Concurrency monitors condition synchronization 13 Magee Kramer 2nd Edition Thread F C Thread F E Thread E B monitor lock Monitor Thread B Thread A data wait notify Thread A C Thread A wait Concurrency monitors condition synchronization 14 Magee Kramer 2nd Edition condition synchronization in Java FSP when cond act NEWSTAT Java public synchronized void act throws InterruptedException while cond wait modify monitor data notifyAll The while loop is necessary to retest the condition cond to ensure that cond is indeed satisfied when it re enters the monitor notifyall is necessary to awaken other thread s that may be Concurrency monitors condition synchronization 15 waiting to enter the monitor now that the monitor data has been changed Magee Kramer 2nd Edition CarParkControl condition synchronization class CarParkControl protected int spaces protected int capacity CarParkControl int n capacity spaces n synchronized void arrive throws InterruptedException while spaces 0 wait spaces notifyAll synchronized void depart throws InterruptedException while spaces capacity wait spaces notifyAll Is it safe to use notify here rather than notifyAll 16 Concurrency monitors condition synchronization Magee Kramer 2nd Edition models to monitors summary Active entities that initiate actions are implemented as threads Passive entities that respond to actions are implemented as monitors Each guarded action in the model of a monitor is implemented as a synchronized method which uses a while loop and wait to implement the guard The while loop condition is the negation of the model guard condition Changes in the state of the monitor are signaled to waiting threads using notify or notifyAll Concurrency monitors condition synchronization 17 Magee Kramer 2nd Edition 5 2 Semaphores Semaphores are widely used for dealing with inter process synchronization in operating
View Full Document
Unlocking...