如何用java开发一个类似生产者消费者的应用?

发布于 2024-12-06 14:38:43 字数 370 浏览 0 评论 0原文

我必须开发一个类似于java中的生产者-消费者问题的应用程序。

不过我对java不太了解,有几个问题想请教一下。

生产者和消费者都是不同的线程,并且它们都需要访问相同的缓冲区。如果它们都是不同的类(扩展线程类或实现可运行接口),我如何编码它们以使用完全相同的缓冲区(这个假定的缓冲区是某个对象的数组)?

我还想阅读一些有关整体架构以及我应该如何实现它们的建议。我需要对它们进行编码,以便两个线程不会同时消耗相同的缓冲区位置,两个生产者线程不会同时插入完全相同的值,生产者无法在缓冲区已经填满,并且当缓冲区为空时,任何消费者都不应该消费。

在这个例子中,必须有多个消费者和多个生产者同时工作。

我在java中查找了一些示例,但它们与我需要的都不同。

I have to develop an application that is similar to the producer-consumer problem in java.

However I do not know a lot about java, and I have a couple of questions.

Both the producer and consumer that are different threads and they both need to access the same buffer. If they are both different classes (that either extends thread class or implement the runnable interface) how do I code them to use the exact same buffer (this supposed buffer is an array of a certain object)?

I also would like to read some suggestions about how the overall architecture and how should I implement them. I need to code them so that two threads don't consume the same buffer position at the same time, that two producer threads don't insert at the exact same value at the same time, producer can't insert a new item in an already filled buffer, and that no consumer should consume when the buffer is empty.

In this example there must be several consumers and several producers working at the same time.

I looked for some examples in java but they are all different of what I need.

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

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

发布评论

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

评论(2

流殇 2024-12-13 14:38:43

您可以通过构造函数将数组或列表的同一实例传递给消费者和生产者。

Array a = new Array();
Consumer c = new Consumer(a);
Producer p = new Producer(a);

对于第二个问题,您想了解(google一下!)Java中的同步。您可以再次将相同的 private Object lock1 = new Object(); 传递给消费者和生产者,他们可以将其用作共享锁。

http://download.oracle.com/javase/tutorial/essential/concurrency /locksync.html

每当消费者或生产者访问共享数组时,他们都需要首先获取锁。其他条件要求,例如“数组已满时不插入元素”或“数组为空时不消耗元素”可以在同步块内实现。

public void add(Object someObject){
    synchronized (lock1) {
        if(a.size()>limit) {
            System.out.println("Array is full");
        } else {
           a.add(someObject)
        }
    }
}

You can pass in a same instance of array or list to both consumer and producer by passing it through their constructor.

Array a = new Array();
Consumer c = new Consumer(a);
Producer p = new Producer(a);

For the second question, you would like to learn about (google it!) for synchronization in Java. You can again pass in the same private Object lock1 = new Object(); to both consumer and producer and they can use it as a shared lock.

http://download.oracle.com/javase/tutorial/essential/concurrency/locksync.html

Whenever a consumer or a producer access the shared array, they would need to acquire lock first. Other conditional requirements such as 'not inserting elements when the array is full' or 'not consuming elements when the array is empty' can be implemented inside the synchronized block.

public void add(Object someObject){
    synchronized (lock1) {
        if(a.size()>limit) {
            System.out.println("Array is full");
        } else {
           a.add(someObject)
        }
    }
}
无尽的现实 2024-12-13 14:38:43

确实在java核心库(1.5以上版本)中,已经有满足你需求的数据结构了。在java.util.concurrent包下,BlockedQueue、LinkedBlockedQueue..等都是并发使用的。

Indeed in the java core library(version 1.5 or above),there are already data structures to meet your needs.Under the java.util.concurrent package,BlockedQueue,LinkedBlockedQueue..etc are all for concurrent using.

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