Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Help with Bounded Buffer problem

  1. #1
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default 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:
    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:
    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:
    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:
    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:
     
    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();
     
    	}
     
    }


  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Bounded Buffer problem

    Hi friends.

  3. #3
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Help with Bounded Buffer problem

    Quote Originally Posted by MangaRao View Post
    Hi friends.
    Thanks thats great help

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Help with Bounded Buffer problem

    Quote Originally Posted by Norm View Post
    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?

  6. #6
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default 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.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.

    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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. java buffer problem florida tech
    By computerbuff in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 12th, 2011, 09:40 PM
  2. Java Frame Buffer Line drawing
    By tcmei in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 17th, 2011, 06:55 AM
  3. How to seperate several messages in a byte buffer
    By perl0101 in forum Java Networking
    Replies: 4
    Last Post: April 12th, 2011, 05:29 PM
  4. PLEASE HELP ME with double buffer
    By DouboC in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2011, 05:32 PM
  5. The Bounded Buffer Problem
    By skatescholar in forum Java Theory & Questions
    Replies: 0
    Last Post: April 8th, 2010, 11:22 AM

Tags for this Thread