java producer-consumer monitor from: concurrent programming: the java programming language by steven...
TRANSCRIPT
Java Producer-Consumer Monitor
From:
Concurrent Programming: The Java Programming Language
By Steven J. Hartley
Oxford University Press , 1998
Declare The Monitor (A Class)
class BoundedBuffer {
Shared Data
// designed for a single producer thread
// and a single consumer thread
private int numSlots = 0;
private double[] buffer = null;
private int putIn = 0, takeOut = 0;
private int count = 0;
public BoundedBuffer(int numSlots) {
if (numSlots <= 0) throw new IllegalArgumentException("numSlots<=0");
this.numSlots = numSlots;
buffer = new double[numSlots];
System.out.println("BoundedBuffer alive, numSlots=" + numSlots);
}
Initializer Entry Method
Producer Entry Method
public synchronized void deposit(double value) {
while (count == numSlots)
try {
wait();
} catch (InterruptedException e) {
System.err.println("interrupted out of wait");
}
buffer[putIn] = value;
putIn = (putIn + 1) % numSlots;
count++; // wake up the consumer
if (count == 1) notify(); // since it might be waiting
System.out.println(" after deposit, count=" + count
+ ", putIn=" + putIn);
}
Consumer Entry Method
public synchronized double fetch() {
double value;
while (count == 0)
try {
wait();
} catch (InterruptedException e) {
System.err.println("interrupted out of wait");
}
value = buffer[takeOut];
takeOut = (takeOut + 1) % numSlots;
count--; // wake up the producer
if (count == numSlots-1) notify(); // since it might be waiting
System.out.println(" after fetch, count=" + count
+ ", takeOut=" + takeOut);
return value;
}
End The Monitor Declaration
} // End the class (just like any other)
Java Fair Reader-Writer Monitor
From:
Concurrent Programming: The Java Programming Language
By Steven J. Hartley
Oxford University Press , 1998
Declare The Monitor (A Class)
class Database extends MyObject {
Shared Data
private int numReaders = 0;
private int numWriters = 0;
private int numWaitingReaders = 0;
private int numWaitingWriters = 0;
private boolean okToWrite = true;
private long startWaitingReadersTime = 0;
public Database() { super("rwDB"); } // Call the parent class’s constructor
Initializer Entry Method
startRead Entry Method
public synchronized void startRead(int i) {
long readerArrivalTime = 0;
if (numWaitingWriters > 0 || numWriters > 0) {
numWaitingReaders++;
readerArrivalTime = age();
while (readerArrivalTime >= startWaitingReadersTime)
try {wait();} catch (InterruptedException e) {}
numWaitingReaders--;
}
numReaders++;
}
endRead Entry Method
public synchronized void endRead(int i) {
numReaders--;
okToWrite = numReaders == 0;
if (okToWrite) notifyAll();
}
startWrite Entry Method
public synchronized void startWrite(int i) {
if (numReaders > 0 || numWriters > 0) {
numWaitingWriters++;
okToWrite = false;
while (!okToWrite)
try {wait();} catch (InterruptedException e) {}
numWaitingWriters--;
}
okToWrite = false;
numWriters++;
}
endWrite Entry Method
public synchronized void endWrite(int i) {
numWriters--; // ASSERT(numWriters==0)
okToWrite = numWaitingReaders == 0;
startWaitingReadersTime = age();
notifyAll();
}
End The Monitor Declaration
} // End the class (just like any other)