Concurrent Programming (19530-V)

Dr. Richard S. Hall WS 01/02

- Übung 5 -

Assigned: 27.11.2001
Due: 4.12.2001

1. Exercise

a) How is a monitor similar to a Java class? How is it different?
b) What is the purpose of condition variables?
c) What must be true before a thread can call wait() or notify()?
d) What two things happen when a thread calls wait()?
e) Why do we usually use condition variables within a while loop?

2.  Exercise

The solution to the musuem example from Übung 2 is reproduced below; identify which of the processes, EAST, WEST, CONTROL, and DIRECTOR, should be threads and which should be monitors. Provide an implementation of the monitors.

    const N=2

EAST = (arrive->EAST).

WEST = (leave->WEST).

DIRECTOR = (open->close->DIRECTOR).

CONTROL = (open->OPENED[0]),
OPENED[i:0..N] = (close->CLOSING[i]
|when (i<N) arrive->OPENED[i+1]
|when (i>0) leave->OPENED[i-1]),
CLOSING[i:0..N] = (when (i==0) lock->CONTROL
|when (i>0) leave->CLOSING[i-1]).

||MUSEUM = (EAST || WEST || DIRECTOR || CONTROL).

3.  Exercise

FSP allows multiple processes to synchronize on a single action. A set of processes with the action sync in their alphabets must all perform this action before any of them can proceed. Implement a monitor called Barrier in Java with a sync method that ensures that N threads must call sync before any of them can proceed.