views:

674

answers:

4

I am working on BoundedBuffer class in consumer and producer we want to use the Semaphore in that class we did that but there are an error in every use of acquire() the rerror is:

unreported exception java.lang.InterruptedException; must be caught or declared to be thrown

Here is the code:

import java.util.concurrent.Semaphore;

public class BoundedBuffer implements Buffer { 
    private static final int   BUFFER_SIZE = 4;

    /**
     * volatile does not appear in the printed text. A discussion of
     * volatile is in chapter 7.
     */
    private volatile int count;
    private Object[] buffer;
    private int in;   // points to the next free position in the buffer
    private int out;  // points to the next full position in the buffer

    private Semaphore  mutex;
    private Semaphore  empty;
    private Semaphore full;

    public BoundedBuffer() { //constractur
        // buffer is initially empty
        //count = 0;
        in = 0;
        out = 0;

        buffer = new Object[BUFFER_SIZE];

        mutex = new Semaphore(1);
        empty = new Semaphore(BUFFER_SIZE);
        full = new Semaphore(0);
    }

    // producer calls this method
    public void insert(Object item) {
        //while (count == BUFFER_SIZE) 
        // ; // do nothing the brach full

        // add an item to the buffer
        // ++count;

        empty.acquire();
        mutex.acquire();
        buffer[in] = item;
        in = (in + 1) % BUFFER_SIZE;//that to do cyrcle or to go to the begining againe
/*
        if (count == BUFFER_SIZE)
            System.out.println("Baker put " + item + " Shelf FULL");
        else
            System.out.println("Baker put " + item + " Shelf Size = " +  count);
*/


        mutex.release();
        full.release();

    }

    // consumer calls this method
    public Object remove() {
        //Object item;
        full.acquire();
        mutex.acquire();

        //while (count == 0) 
            ; // do nothing the buffer is empty

        // remove an item from the buffer
        //--count;

        Object item = buffer[out];
        out = (out + 1) % BUFFER_SIZE;
        mutex.release();
        empty.release();
        return item;
    }
}
+2  A: 

Maybe I do not understand your application completely, but could not you just use the bounded buffer class that is already provided in the java.util.concurrent package (ArrayBlockingQueue)?

This is a classic "bounded buffer", in which a fixed-sized array holds elements inserted by producers and extracted by consumers. Once created, the capacity cannot be increased. Attempts to put an element to a full queue will result in the put operation blocking; attempts to retrieve an element from an empty queue will similarly block.

Thilo
+1  A: 

The error tells you all you need to know; InterruptedException may be thropwn by acquire - hence you need to either a) catch it and handle it or b) allow it to propagate out of the calling function - necessitating you adding it to the functions throws sepcification.

Visage
+1  A: 

You need to handle exceptions thrown by the acquire method.

McDowell
A: 

I used this BoundedBuffer. I created 2 buffers. Buffer1 and Buffer2. I was able to insert only into the first buffer(buffer1). Later, ı couldn't insert into the second one(buffer2). İs there anyone who knows the reason? Please Help me.

Burak