Help with Bounded Buffer problem
Hey there,
Been working on an assignment whereby I have to create a bounded buffer in Java using threads. I'll briefly state the main specs for the assignment instead of pasting the whole thing:
- It operates in a FIFO manner (First In First Out)
- We cannot use semaphores & can only use synchronized, wait, notifyAll, etc
- Updates to the buffer must be mutually exclusive
- It must contain the following classes : BoundedBuffer, Producer, Consumer, Watcher
I have a lot of it attempted but I'm having a few issues with the data being produced. I find that the updated status on the buffer data being produced by the Watcher thread seems to be out of sync or inaccurate in regards to what is actually happening. Everything seems to be a bit messy. Also, I'm not sure if my modulo arithmetic is doing the right job to ensure the pointers are being wrapped. Can anybody tell me if I'm doing things the right way and if not, can even nudge me in the right direction I'll post my code below. I understand that it's a lot of code to look through but i'd really be grateful with any help.
BoundedBuffer:
Code Java:
public class BoundedBuffer {
// Variables
private int nextIn, nextOut, size, occupied, ins, outs;
private boolean dataAvailable, roomAvailable;
private int[] buffer;
// Constructor
public BoundedBuffer(int size) {
this.size = size;
buffer = new int[size]; // Set buffer size
// Initialize variables
nextIn = nextOut = occupied = ins = outs = 0;
dataAvailable = false;
roomAvailable = true;
}
public synchronized void insertItem(int item) {
while (roomAvailable == false || occupied == size) {
try {
wait();
} catch (InterruptedException e) {
System.out.println("Error inserting item.");
}
}
dataAvailable = true;
notifyAll();
buffer[nextIn] = item;
// For debugging purposes
System.out.println("Item: " + item + " inserted.");
nextIn = (nextIn + 1) % size;
occupied++;
ins++;
}
public synchronized int removeItem() {
while (dataAvailable == false || occupied == 0) {
try {
wait();
} catch (InterruptedException e) {
System.out.println("Error removing item.");
}
}
roomAvailable = true;
notifyAll();
int item = buffer[nextOut];
// For debugging purposes
System.out.println("Item: " + item + " removed.");
occupied--;
nextOut = (nextOut + 1) % size;
outs++;
return item;
}
public void printStatus() {
System.out.println("Items inserted : " + ins);
System.out.println("Items removed : " + outs);
System.out.println("Spaces occupied : " + occupied);
System.out.println("Delta : " + (ins - outs - occupied));
System.out.println();
}
}
Producer:
Code Java:
public class Producer extends Thread {
private BoundedBuffer buffer;
private int item;
public Producer(BoundedBuffer buffer) {
this.buffer = buffer;
}
public void run() {
try {
while (true) {
item = (int) (Math.random() * 100);
buffer.insertItem(item);
// Sleep for 2 seconds
sleep(2000);
}
} catch (InterruptedException e) {
System.out.println("Error with Producer Thread.");
}
}
}
Consumer:
Code Java:
public class Consumer extends Thread {
private BoundedBuffer buffer;
public Consumer(BoundedBuffer buffer) {
this.buffer = buffer;
}
public void run() {
try {
while (true) {
buffer.removeItem();
// Sleep for 2 seconds
sleep(2000);
}
} catch (InterruptedException e) {
System.out.println("Error with Consumer Thread.");
}
}
}
Watcher:
Code Java:
public class Watcher extends Thread {
private BoundedBuffer buffer;
public Watcher(BoundedBuffer buffer) {
this.buffer = buffer;
}
public void run() {
try {
while (true) {
buffer.printStatus();
sleep(2000);
}
} catch (InterruptedException e) {
System.out.println("Error in Watcher thread.");
}
}
}
Assignment1:
Code Java:
public class Assignment1 {
public static void main(String[] args) {
BoundedBuffer buffer = new BoundedBuffer(20);
Producer prod = new Producer(buffer);
Consumer cons = new Consumer(buffer);
Watcher watch = new Watcher(buffer);
prod.start();
cons.start();
watch.start();
}
}
Re: Help with Bounded Buffer problem
Re: Help with Bounded Buffer problem
Quote:
Originally Posted by
MangaRao
Hi friends.
Thanks thats great help
Re: Help with Bounded Buffer problem
Try debugging the code by adding printlns that print out the values of variables as they are changed and used. If you know what the code is supposed to do, seeing what it is doing should help you find and fix the problem.
Re: Help with Bounded Buffer problem
Quote:
Originally Posted by
Norm
Try debugging the code by adding printlns that print out the values of variables as they are changed and used. If you know what the code is supposed to do, seeing what it is doing should help you find and fix the problem.
Cheers, I've added in some printlns that print out each variable as soon as any change is made and it seems to be giving the correct results. It's only when I call the printStatus() method in the watcher thread that seems to give slightly unaccurate or out of sync results. For instance it might say the number of occupied spaces is 1 when it really should be 0. It's as if it can be a bit late. Is there a way to stop this?
Also, we are told the threads have to be terminated after 1 minute and we're not allowed to use thread.stop() to do this. Any ideas?
Re: Help with Bounded Buffer problem
In multi-threading you want to thread to exit gracefully. thread.stop() does not do that. Look for the answer in the api for thread.stop() and why they deprecated the method.
Re: Help with Bounded Buffer problem
A Thread ends when the code using it returns from the run() method.
Your testing model doesn't show anything. Several changes I'd recommend:
Change the sleep time to 100 ms or less. 2 seconds is much too long.
Change the sleep time for the producer to be 1/2 that for the others
Define one global variable with the sleep time and have all the calls to sleep() use it.
Change the size of the buffer to a small value say 3 vs 20.
Have the Watcher use a for loop for a small number of times (100) and have it call System.exit(0); when it falls out of the loop.
You need to have the program generate a small amount of print out in a short time that can easily be analyzed.
Then change some of the above parameters of the testing and see what happens.
Quote:
number of occupied spaces is 1 when it really should be 0. It's as if it can be a bit late. Is there a way to stop this?
The printouts should show you why that is happening. Add enough so you see every time the value is changed.