当我们在消费者和生产者缓冲区中使用信号量时

时间:2022-09-11 11:45:31

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:

我正在使用消费者和生产者的BoundedBuffer类我们想在那个类中使用Semaphore,但是在每次使用acquire()时都会出现错误,rerror是:

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

未报告的异常java.lang.InterruptedException;必须被抓住或宣布被抛出

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;
    }
}

3 个解决方案

#1


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)?

也许我完全不了解你的应用程序,但是你不能只使用java.util.concurrent包(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.

这是一个经典的“有界缓冲区”,其中固定大小的数组包含由生产者插入并由消费者提取的元素。一旦创建,容量就无法增加。尝试将元素放入完整队列将导致put操作阻塞;尝试从空队列中检索元素同样会阻止。

#2


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.

错误告诉你所有你需要知道的事情; InterruptedException可能会被获取 - 这需要a)捕获它并处理它或b)允许它传播出调用函数 - 需要你将它添加到函数抛出sepcification。

#3


You need to handle exceptions thrown by the acquire method.

您需要处理acquire方法抛出的异常。

#1


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)?

也许我完全不了解你的应用程序,但是你不能只使用java.util.concurrent包(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.

这是一个经典的“有界缓冲区”,其中固定大小的数组包含由生产者插入并由消费者提取的元素。一旦创建,容量就无法增加。尝试将元素放入完整队列将导致put操作阻塞;尝试从空队列中检索元素同样会阻止。

#2


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.

错误告诉你所有你需要知道的事情; InterruptedException可能会被获取 - 这需要a)捕获它并处理它或b)允许它传播出调用函数 - 需要你将它添加到函数抛出sepcification。

#3


You need to handle exceptions thrown by the acquire method.

您需要处理acquire方法抛出的异常。