School of Computing and Information Systems The University of Melbourne
SWEN90004 Modelling Complex Software Systems
Concurrency Workshop 2 solutions
The exercises
Copyright By Assignmentchef assignmentchef
1. This sample solution uses a boolean variable, waiting, which keeps track of whether there is a thread waiting (as such, it is initialised to false). Initially, and whenever waiting is false, the first thread that calls synch will set waiting to true and suspend itself by calling wait(). After that, the second thread that calls synch will wake up the suspended thread by calling notify() and set waiting back to false, so that the next time around the loop the first process that calls synch() will get suspended again.
class Synchronise
// flags whether one process is waiting for the other
boolean waiting = false;
public synchronized void synch()
// if the other thread got here first, it is waiting, so prod it
if (waiting) {
waiting = false;
// otherwise it has not arrived here yet, so wait for it
// let the other thread know that we are here, waiting
waiting = true;
catch (InterruptedException e) {
Thread.currentThread().interrupt();
2. To complete the buffer using wait() and notifyAll(), make four changes:
(a) Modify put so that the monitor forces the calling thread to wait when the buffer is full.
Therefore, remove
if (buffer.size() < MAXSIZE)buffer.add(input);and replace it with:while (buffer.size() >= MAXSIZE)
buffer.add(input);
(b) After a new item has been added to the buffer, notify all waiting threads that the buffer has changed by adding notifyAll(); at the end of put.
(c) Modify get so that the monitor forces the calling thread to wait when the buffer is empty. Therefore, add the following at the start of get:
while (buffer.size() == 0)
(d) After an item has been removed from the buffer, notify all waiting threads that the buffer has changed by adding notifyAll(); at the end of get.
3. To complete the buffer using Javas Semaphore class, make the following changes (to the original version of UseBuffer.java provided):
(a) Import java.util.concurrent.Semaphore
(b) Add three semaphores to BoundedBuffer:
private Semaphore mutex = new Semaphore(1);
private Semaphore notEmpty = new Semaphore(0);
private Semaphore notFull = new Semaphore(MAXSIZE);
(c) Modify put to acquire the notFull and mutex semaphores before the call to add input. Remember that acquire throws an InterruptedException. Note that the if condition can be removed, as acquiring the notFull semaphore will block if the buffer is full. After adding the input, release the mutex and notEmpty semaphores:
notFull.acquire();
mutex.acquire();
} catch (InterruptedException e) {}
buffer.add(input);
mutex.release();
notEmpty.release();
(d) Modify get to acquire the notEmpty and mutex before the call to remove to obtain the result. Again, these will need to be wrapped in a try-catch block. After removing the result, release the mutex and notFull semaphores.
(e) Remove the synchronised keywords from the put and get methods (you are now han- dling mutual exclusion yourself, using the semaphores).
CS: assignmentchef QQ: 1823890830 Email: [email protected]
Reviews
There are no reviews yet.