Bounded Buffer

Description

A classical bounded buffer composed of cells. The buffer is shared by a producer and a consumer. The producer can only write in an empty cell, and the consumer can only read a full cell. This version is deadlock free.

Run it!

Scripts

Unix ProActive/scripts/unix/ buffer.sh None
Windows ProActive/scripts/windows/ buffer.bat None

Screenshot of the interface

Design

Active Objects

BoundedBuffer - The bounded buffer public class Boundedbuffer implements Active
Producer - The producer
public class Producer implements Active Consumer - The consumer
public class Consumer implements Active ActiveDisplay - The distributed display
public class ActiveDisplay implements Active

Object Graph

Class Interface Inheritance

...

Source Code

Main Parts

Bounded Buffer

This class is responsible for the whole synchronization of the application. Here is the synchronization code:

Class BoundedBuffer - Live method
  /**
* The only synchronization method
*/
public void live(org.objectweb.proactive.Body body) {
org.objectweb.proactive.Service service = new org.objectweb.proactive.Service(body);
while (body.isActive()) {
if (count == 0) {
// if the buffer is empty
service.blockingServeOldest("put"); // Serve the first buffer.put call
} else if (count == size) {
// if the buffer is full
service.blockingServeOldest("get"); // Serve the first buffer.get call
} else {
// if the buffer is neither empty nor full
service.blockingServeOldest(); // Serve the first buffer.xxx call
}
}
}

Producer

The role of the producer is to fill the buffer's cells with data [String] in a synchronous way. The producer will fill one cell after another then pause for some time. The role of the consumer is to empty the buffer's cells in a synchronous way. The producer will read one cell after another then pause for some time.

Consumer / Producer - Live method
  /**
* The only synchronization method
*/
public void live(org.objectweb.proactive.Body body) {
org.objectweb.proactive.Service service = new org.objectweb.proactive.Service(body);
while (isActive) {
// Allow the display to toggle or kill the consumer
service.serveOldest();
boolean wasSuspended = isSuspended;
if (requestChange) {
isSuspended = !isSuspended;
requestChange = false;
}
// Try to get some datas
doStuff(wasSuspended);
long l = 500 + (long)(Math.random() * 1000);
//listener.displayMessage(name+" now sleeping for "+l+" ms");
try {
Thread.sleep(l);
} catch (InterruptedException e) {}
}
}

Full Sources

Source code index


Feel free to send any suggestions to proactive@objectweb.org © 2001-2007 Inria Sophia Antipolis