Results 1 to 1 of 1
Thread: Buffer Example help.
- 11-27-2009, 09:26 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 6
- Rep Power
- 0
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.
Java 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 [COLOR="Red"] Consumer c = new Consumer(b); Producer p = new Producer(b);[/COLOR] //launch producer and consumer threads [COLOR="Red"] c.start(); p.start();[/COLOR] try{ Thread.sleep(20000);} catch(InterruptedException e){System.exit(0);} p.interrupt(); c.interrupt(); System.out.println("system timed out"); } }Java 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 }Java 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 [COLOR="Red"]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[/COLOR] } public synchronized Object get() throws InterruptedException { //implement conditional synchronization // wait if buffer is empty [COLOR="Red"] while(buf == null){ wait(); } [/COLOR] 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); } }Java 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");} } }Im totally lost and have no idea where to look.Java 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); [COLOR="Red"]buf.put(r);[/COLOR] //place integer object into buffer } } catch(InterruptedException e ){System.out.println("received interrupt");} } }
Regards, Robert.
Similar Threads
-
how to read the same req buffer twice
By leejava in forum New To JavaReplies: 2Last Post: 11-17-2009, 07:29 PM -
Need Help: Double Buffer
By rukawa527 in forum AWT / SwingReplies: 1Last Post: 02-09-2009, 12:39 AM -
Buffer
By QPRocky in forum NetworkingReplies: 2Last Post: 02-02-2009, 03:54 PM -
Help with String Buffer
By mathias in forum AWT / SwingReplies: 1Last Post: 08-07-2007, 06:52 AM -
how to set the value of BUFFER SIZE
By oregon in forum Advanced JavaReplies: 1Last Post: 08-06-2007, 03:16 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks