G52CON: Concepts of Concurrency Lecture 11: Monitors in Java Abdur Rakib School of Computer Science [email protected] © Abdur Rakib 2012 G52CON Lecture 11: Monitors in Java 1 Content • Monitors in Java • Bounded buffer producer-consumer problem - Solution with classical monitors - Solution with Java monitors • Exercise: Saving accounts problem - Solution with classical monitors - Solution with Java monitors © Gabriela Ochoa 2011, © Abdur Rakib 2012 G52CON Lecture 11: Monitors in Java 2 Monitors in Java • In Java there is no special construct for a monitor. Every object has a lock that can be used for synchronising access to fields of the objects class PCMonitor { final int N = 5; int Oldest = 0, Newest = 0; volatile int Count = 0; int Buffer [] = new int[N]; synchronized int Take() { int temp; while (Count == 0) try { wait(); } catch (InterruptedException e) {} synchronized void Append(int V) { temp = Buffer[Oldest]; while (Count == N) Oldest = (Oldest + 1) % N; try { wait(); Count = Count - 1; } catch (InterruptedException e) {} notifyAll (); Buffer [ Newest] = V; return temp; Newest = (Newest + 1) % N; } Count = Count + 1; } notifyAll (); } © Abdur Rakib 2012 G52CON Lecture 11: Monitors in Java 3 Monitors in Java • Once a class is defined we can declare objects of this class - PCMonitor monitor = new PCMonitor • And then invoke its methods - nonitor.Append(5); - int n = monitor.Take(); © Gabriela Ochoa 2011 G52CON Lecture 11: Monitors in Java 4 Monitors in Java • The synchronized specifier in the declaration of a method, means that the process must acquire the lock for that object before executing the method • Upon return from (the last) synchronized method invoked on an object, the lock is released • A class whose methods are declared synchronized can be called a Java monitor • Difference from classical monitors: A Java monitor has no condition variables. Instead it has s single implicit condition variable per object. © Gabriela Ochoa 2011 G52CON Lecture 11: Monitors in Java 5 Monitors in Java • Java provides built-in support for condition synchronisation with the methods wait(), notify() and notifyAll(): • If a process needs to block itself (i.e. to avoid appending to a full buffer, or taking from an empty buffer) it can invoke the method wait() • wait() throws InterruptedException , that needs to be caught • notify(): similar to signal(v)in the classic monitors, it will unblock one process • notifyAll(): similar to signal_all(v). It will unblock all processes © Gabriela Ochoa 2011 G52CON Lecture 11: Monitors in Java 6 wait(), notify()and notifyAll() • wait() , notify() and notifyAll() must be executed within synchronized methods or blocks • wait() releases the lock on the object held by the calling thread-the thread blocks and is added to the wait set for the object • notify() wakes up a thread in the wait set (if any) • notifyAll() wakes up all threads in the wait set (if any) © Brian Logan 2007 G52CON Lecture 11: Monitors in Java 7 notify() vs notifyAll() • notify() can be used to increase performance when only one thread needs to be woken: - all threads in the wait set are waiting on the same delay condition and - each notification enables at most one thread to continue and • notifyAll() is required when: - the threads in the wait set are waiting on different conditions; or - a notification can satisfy multiple waiting threads © Brian Logan 2007 G52CON Lecture 11: Monitors in Java 8 Summary: Monitors & Java • A monitor in Java is a class whose methods are declared synchronized • A synchronized method (or block) is a monitor operation • Each Java object has a single (implicit) condition variable and delay queue, the wait set, manipulated by wait(), notify(), notifyAll() • Since there is only one implicit condition variable per object, wait, notify and notifyall have no arguments. • Java uses the signal and continue signalling discipline © Brian Logan 2007 Chris Greenhalgh 2010 Gabriela Ochoa 2011 G52CON Lecture 11: Monitors in Java 9 PRODUCER CONSUMER WITH BOUNDED BUFFER © Gabriela Ochoa 2011 G52CON Lecture 11: Monitors in Java 10 Producer-Consumer problem produce data item Producer put it in the buffer Buffer take item from the buffer Consumer consume (use) data item © Gabriela Ochoa 2011 G52CON Lecture 11: Monitors in Java 11 The producer-consumer problem • There are two synchronisation issues in the producer-consumer problem with bounded buffer 1. A producer cannot append a data element to a full buffer 2. A consumer can not take an element from an empty buffer © Gabriela Ochoa 2011 G52CON Lecture 11: Monitors in Java 12 Bounded buffer with classical monitors monitor BoundedBuffer { // Private variables … Object buf = new Object[n]; integer out = 0, // index of first full slot in = 0, // index of first empty slot count = 0; // number of full slots // Condition variables ... condvar not_full, // signalled when count < n not_empty; // signalled when count > 0 // continued ... © Brian Logan 2007 G52CON Lecture 11: Monitors in Java 13 Bounded Buffer with classical monitors 2 // Monitor procedures ... procedure append(Object data) { while(count == n) { wait(not_full); } buf[in] = data; in = (in + 1) % n; count++; signal(not_empty); } © Brian Logan 2007 procedure remove(Object &item) { while(count == 0) { wait(not_empty); } item = buf[out]; out = (out + 1) %n; count--; signal(not_full); } } G52CON Lecture 11: Monitors in Java 14 Bounded buffer in Java class BoundedBuffer { // Private variables … private Object buf; private int out = 0, // index of first full slot in = 0, // index of first empty slot count = 0; // number of full slots // No separate condition variables public BoundedBuffer(int n) { buf = new Object[n]; } // continued ... © Brian Logan 2007 G52CON Lecture 11: Monitors in Java 15 Bounded buffer in Java 2 public synchronized void append(Object data) { try { while(count == n) { wait(); } catch (InterruptedException e) { return; } buf[in] = data; in = (in + 1) % n; count++; notifyAll(); } © Brian Logan 2007 G52CON Lecture 11: Monitors in Java 16 Bounded buffer in Java 3 public synchronized Object remove() { try { while(count == 0) { wait(); } catch (InterruptedException e) { return null; } Object item = buf[out]; out = (out + 1) % n; count--; notifyAll(); return item; } } © Brian Logan 2007 G52CON Lecture 11: Monitors in Java 17 Savings account problem • A savings account is shared by several people • Each person may deposit or withdraw money from the account • The current balance in the account is the sum of all deposits to date less the sum of all withdrawals to date • The balance must never become negative • Account holders are represented as processes • A process making a deposit never has to delay (except for mutual exclusion), but a withdrawal has to wait until there are sufficient funds © Gabriela Ochoa 2011 G52CON Lecture 11: Monitors in Java 18 Savings account with monitors • Develop a monitor to solve this problem • The monitor should have two procedures: - deposit(amount) - withdraw(amount). • Assume the arguments to deposit and withdraw are positive, and the monitor uses Signal and Continue • Exercise 3: Solution with classical monitors • Exercise 4: Solution with Java monitors © Gabriela Ochoa 2011 G52CON Lecture 11: Monitors in Java 19 Next lecture Synchronisation in Java 1 Suggested reading: Ben-Ari (2006), chapter 7 Lea (2000), chapter 2 © Gabriela Ochoa 2011 G52CON Lecture 11: Monitors in Java 20
© Copyright 2024 ExpyDoc