Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-27-2009, 10:26 AM
Member
 
Join Date: Oct 2009
Posts: 6
Rep Power: 0
bert682 is on a distinguished road
Default Buffer Example help.
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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to read the same req buffer twice leejava New To Java 2 11-17-2009 08:29 PM
Need Help: Double Buffer rukawa527 AWT / Swing 1 02-09-2009 01:39 AM
Buffer QPRocky Networking 2 02-02-2009 04:54 PM
Help with String Buffer mathias AWT / Swing 1 08-07-2007 07:52 AM
how to set the value of BUFFER SIZE oregon Advanced Java 1 08-06-2007 04:16 AM


All times are GMT +2. The time now is 07:03 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org