DOC PREVIEW
CU-Boulder CSCI 5828 - Monitors & Condition Synchronization

This preview shows page 1-2-3-19-20-39-40-41 out of 41 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1©Magee/Kramer 2nd EditionCSCI 5828: Foundationsof Software EngineeringLecture 16: Monitors &Condition SynchronizationSlides created by Mageeand Kramer for theConcurrency textbookConcurrency: monitors & condition synchronization 2©Magee/Kramer 2nd EditionChapter 5Monitors &Condition SynchronizationConcurrency: monitors & condition synchronization 3©Magee/Kramer 2nd Editionmonitors & condition synchronizationConcepts: monitors:encapsulated data + access proceduresmutual exclusion + condition synchronization single access procedure active in the monitor nested monitorsModels: guarded actionsPractice: private data and synchronized methods (exclusion). wait(), notify() and notifyAll() for condition synch. single thread active in the monitor at a timeConcurrency: monitors & condition synchronization 4©Magee/Kramer 2nd Edition5.1 Condition synchronizationA controller is required for a carpark, which only permits cars to enterwhen the carpark is not full and does not permit cars to leave whenthere are no cars in the carpark. Car arrival and departure are simulatedby separate threads.Concurrency: monitors & condition synchronization 5©Magee/Kramer 2nd Editioncarpark model♦ Events or actions of interest?arrive and depart♦ Identify processes.arrivals, departures and carpark control♦ Define each process and interactions (structure).ARRIVALSCARPARKCONTROLDEPARTURESarrivedepartCARPARKConcurrency: monitors & condition synchronization 6©Magee/Kramer 2nd Editioncarpark modelCARPARKCONTROL(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 7©Magee/Kramer 2nd Editioncarpark 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?ARRIVALSCARPARKCONTROLDEPARTURESarrivedepartCARPARKConcurrency: monitors & condition synchronization 8©Magee/Kramer 2nd Editioncarpark program - class diagramAppletRunnableThreadPanelCarPark ControlArrivalsDeparturesDisplayCarParkCarPark Canva sCarParkarrivals,departuresarrive()depart()carDisplaycarparkdispWe have omittedDisplayThreadandGraphicCanvasthreads managed byThreadPanel.Concurrency: monitors & condition synchronization 9©Magee/Kramer 2nd Editionpublic void start() { CarParkControl c = new DisplayCarPark(carDisplay,Places); arrivals.start(new Arrivals(c)); departures.start(new Departures(c));}carpark programArrivals and Departures implement Runnable,CarParkControl provides the control (condition synchronization).Instances of these are created by the start() method of theCarPark applet :Concurrency: monitors & condition synchronization 10©Magee/Kramer 2nd Editioncarpark program - Arrivals and Departures threadsclass Arrivals implements Runnable { CarParkControl carpark; Arrivals(CarParkControl c) {carpark = c;} public void run() { try { while(true) { ThreadPanel.rotate(330); carpark.arrive(); ThreadPanel.rotate(30); } } catch (InterruptedException e){} }}How do we implement the control of CarParkControl?Similarly Departureswhich callscarpark.depart().Concurrency: monitors & condition synchronization 11©Magee/Kramer 2nd EditionCarpark program - CarParkControl monitorclass CarParkControl { protected int spaces; protected int capacity; CarParkControl(int n) {capacity = spaces = n;} synchronized void arrive() { … --spaces; …} synchronized void depart() {… ++spaces; …}}conditionsynchronization?block if full?(spaces==0)block if empty?(spaces==N)mutual exclusionby synch methodsConcurrency: monitors & condition synchronization 12©Magee/Kramer 2nd Editioncondition synchronization in JavaJava 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 InterruptedExceptionWaits 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 monitorbefore resuming execution.Concurrency: monitors & condition synchronization 13©Magee/Kramer 2nd Editioncondition synchronization in JavaWe refer to a thread entering a monitor when it acquires the mutualexclusion lock associated with the monitor and exiting the monitorwhen it releases the lock.Wait() - causes the thread to exit the monitor, permitting other threads to enter the monitor.Thread A Thread Bwait()notify()MonitordataConcurrency: monitors & condition synchronization 14©Magee/Kramer 2nd Editionmonitor lockwait()MonitordatawaitThread CThread EThread BThread FThread Anotify()Thread BThread FThread EThread AThread CThread AConcurrency: monitors & condition synchronization 15©Magee/Kramer 2nd Editioncondition synchronization in JavaFSP: when cond act -> NEWSTATJava: 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 thatcond is indeed satisfied when it re-enters the monitor.notifyall() is necessary to awaken other thread(s) that may bewaiting to enter the monitor now that the monitor data has been changed.Concurrency: monitors & condition synchronization 16©Magee/Kramer 2nd EditionCarParkControl - condition synchronizationclass 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()?Concurrency: monitors & condition synchronization 17©Magee/Kramer 2nd Editionmodels to monitors - summaryEach guarded action in the model of a monitor isimplemented as a synchronized methodwhich uses a while loop and wait()


View Full Document

CU-Boulder CSCI 5828 - Monitors & Condition Synchronization

Documents in this Course
Drupal

Drupal

31 pages

Deadlock

Deadlock

23 pages

Deadlock

Deadlock

23 pages

Deadlock

Deadlock

22 pages

Load more
Download Monitors & Condition Synchronization
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Monitors & Condition Synchronization and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Monitors & Condition Synchronization 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?