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

发布于 2024-07-19 16:11:17 字数 2145 浏览 6 评论 0原文

我正在消费者和生产者中研究 BoundedBuffer 类,我们想在该类中使用信号量 我们这样做了,但是每次使用 acquire() 时都会出现错误 错误是:

未报告的异常 java.lang.InterruptedException; 必须被捕获或声明为抛出

以下是代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

讽刺将军 2024-07-26 16:11:17

也许我不完全理解你的应用程序,但你不能只使用 java.util.concurrent 包中已经提供的有界缓冲区类(ArrayBlockingQueue)?

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

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.

橙幽之幻 2024-07-26 16:11:17

该错误告诉您所有您需要知道的信息; InterruptedException 可能会被 acquire 引发 - 因此您需要 a) 捕获它并处理它,或者 b) 允许它从调用函数中传播 - 需要您将其添加到函数中,从而抛出异常。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文