Hey guys,
Been given this program that was incomplete, I was to add in the missing parts. The idea is that there is a buffer and a producer adding to it and a consumer removing from it. Code is attached below, bits that I have added are in RED.
|
Code:
|
import java.io.*;
public class BoundedBuffer
{
public static void main(String args[])
{
// create buffer
Buffer b = new BufferImpl();
// create producer and consumer threads sharing buffer
Consumer c = new Consumer(b);
Producer p = new Producer(b);
//launch producer and consumer threads
c.start();
p.start();
try{ Thread.sleep(20000);}
catch(InterruptedException e){System.exit(0);}
p.interrupt();
c.interrupt();
System.out.println("system timed out");
}
} |
|
Code:
|
public interface Buffer
{
public void put(Object obj)
throws InterruptedException; //put object into buffer
public Object get()
throws InterruptedException; //get an object from buffer
} |
|
Code:
|
public class BufferImpl implements Buffer {
private Object[] buf; //the buffer,declared as an array of objects
private int in = 0; //index of the next place where an item
//can be added
private int out= 0; //index of the next item that can be removed
private int count= 0; //count of the number of items in the buffer
private int size = 5; //number of slots in the buffer
public BufferImpl() {
buf = new Object[5]; //actual buffer can hold 5 objects
}
public synchronized void put(Object obj) throws InterruptedException {
// implement conditional synchronization
// wait if buffer is full
while(buf.length>4){
wait();
}
obj = buf[in]; //place object into buffer at index 'in'
count++; //increment count of number of items
//in buffer
in++; //increment index 'in'
//update of shared data completed
}
public synchronized Object get() throws InterruptedException {
//implement conditional synchronization
// wait if buffer is empty
while(buf == null){
wait();
}
Object obj = buf[out]; //get object from buffer at index 'out'
buf[out] = null;
size = size - 1; //decrement number of items in buffer
out = (out+1) % size; //increment index 'out'
//update of shared data completed
return (obj);
}
} |
|
Code:
|
class Consumer extends Thread
{
private Buffer buf;
private int r = 0;
Consumer(Buffer b) {buf = b;}
public void run() {
try {
while(true) {
r = ((Integer)buf.get()).intValue();
for (int i = 0; i < 5000000; i++) {r=r*2/2;}
System.out.println("Consume " + r);
}
} catch(InterruptedException e ){System.out.println("received interrupt");}
}
} |
|
Code:
|
public class Producer extends Thread
{
Buffer buf; // the buffer used by the producer
private int r = 2; // the variable on which a calculation is made
Producer(Buffer b) {buf = b;}
public void run() {
try {
while(true)
{
r++; // increment the value of r
for (int j = 0; j < 2000000; j++) { r = r*2/2;}
Integer i = new Integer(r); // create a wrapper object for r
System.out.println("Produce " + r);
buf.put(r);
//place integer object into buffer
}
} catch(InterruptedException e ){System.out.println("received interrupt");}
}
} |
Im totally lost and have no idea where to look.
Regards, Robert.