Questions tagged [producer-consumer]

The Producer-Consumer Problem (also known as the bounded-buffer problem) is a classical example of a multi-process synchronization problem. The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue.

The Producer-Consumer Problem (also known as the bounded-buffer problem) is a classical example of a multi-process synchronization problem. The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue. The producer's job is to generate a piece of data, put it into the buffer and start again. At the same time the consumer is consuming the data (i.e., removing it from the buffer) one piece at a time. The problem is to make sure that the producer won't try to add data into the buffer if it's full and that the consumer won't try to remove data from an empty buffer.

The solution for the producer is to either go to sleep or discard data if the buffer is full. The next time the consumer removes an item from the buffer, it notifies the producer who starts to fill the buffer again. In the same way, the consumer can go to sleep if it finds the buffer to be empty. The next time the producer puts data into the buffer, it wakes up the sleeping consumer. The solution can be reached by means of inter-process communication, typically using semaphores. An inadequate solution could result in a deadlock where both processes are waiting to be awakened. The problem can also be generalized to have multiple producers and consumers.

References

1564 questions
0
votes
4 answers

Can I use one Runnable for Two threads such that one produces and one consumes?

In the code below I want Thread t1 to execute produce of Runnable and Thread t2 to execute consume of Runnable. Will this work ? class Processor implements Runnable { @override run(){ produce(); consume(); } void…
Phoenix
  • 8,695
  • 16
  • 55
  • 88
0
votes
4 answers

Java Producer Consumer Pattern with Consumer completion notification

I want to implement a producer / consumer scenario where i have multiple producers and a single consumer. Producers keep adding items to a queue and consumer dequeues the items. When the consumer has processed enough items, both the producers and…
shadowfax
  • 971
  • 1
  • 10
  • 17
0
votes
0 answers

How can I efficiently use BlockingCollection in this scenario

I am designing solution for multi-threaded Producer Consumer problem. product are very much in numbers (around 100,000) because consumer runs once in an Hour. there are multiple producer threads and one consumer thread. Consumer thread is a Task…
Imran Rizvi
  • 7,331
  • 11
  • 57
  • 101
0
votes
1 answer

Terminating Producer-Consumer threads with wait/notify mechanism in Java

The application I'm maintaining (passed through many coders) has the Producer-Consumer problematic implemented using wait/notify mechanism. Consumer waits for the message on the "server" side of the application and then forwards the message on the…
vobelic
  • 51
  • 1
  • 5
0
votes
4 answers

producer consumer pattern with concurrenthashmap in java

I have the following problem, and I am not sure how to design parts of the solution: I have a large text file that I read line by line. I need to process each line and update a HashMap. AFAIK I need one producer thread to read the lines from the…
0
votes
2 answers

Java Resource-Game Simulation

I'm into a project of a resource-game simulation. What I have to do is... "If there are enough resources available, the request will be satisfied, and the requested quantities will be subtracted from the available quantities. If there are not enough…
0
votes
2 answers

Producer-Consumer using a stack in Java

I am using a stack as my storage to implement my Producer-Consumer model. public class Main { /** * @param args */ public static void main(String[] args) { Stackable stack = new MyArrayStack(); Producer producer = new…
Kevin
  • 6,711
  • 16
  • 60
  • 107
0
votes
1 answer

Why aren't my threads running?

// windows_procon.cpp : Defines the entry point for the console application. #include "stdafx.h" #include #include #include #include #include using namespace std; HANDLE mutex; HANDLE…
user2134127
  • 135
  • 1
  • 2
  • 9
0
votes
2 answers

Queue for multiple producers and consumers

I have to write a communication between multiple threads (at least 3 for now) in .Net3.5 and each of them is both producer and consumer. Instead of sending signals between each pair of threads my idea is to implement a queue of messages which will…
Vladimir Perković
  • 1,351
  • 3
  • 12
  • 30
0
votes
1 answer

Producer-Consumer Issue

Hi I'm trying to write an algorithm for solving the producer-consumer problem and I've hit a roadblock. This is the output I am getting from my code: Producing: 6 6 0 0 0 0 0 0 0 0 0 END and then the program exits. I'm not sure where I went wrong?…
user2134127
  • 135
  • 1
  • 2
  • 9
0
votes
1 answer

Hybrid solution of consumer producer algorithm

I am trying to show that the following solution of the producer/consumer problem does not work, by showing that when a consumer is at the beginning of M1, there is a case when it won't be able to dequeue an item within a finite time, and/or there is…
ratsimihah
  • 1,010
  • 1
  • 11
  • 22
0
votes
1 answer

Difference Flex/Flash NetConnection and Consumer/Producer

What's the difference between a NetConnection and Consumer/Producer in Flex? It seems that they both use RTMP but Consumer/Producer uses Channels and NetConnection does not. Does the Consumer/Producer uses a NetConnection underneath?
Lieven Cardoen
  • 25,140
  • 52
  • 153
  • 244
0
votes
4 answers

Producer-consumers(many). Consumers take and put into the shared queue

I made a producer-consumer program. It's just a program in core java without any GUI(Swing or SWT). It has one producer who put objects into the queue. Also there is a few consumers who must add some staff(for example String) into Every object in…
0
votes
1 answer

Faster consumer in inadequate implementation of classical producer consumer

Wikipedia provides an inadequate implementation of the classical producer-consumer problem. In that implementation, the consumer is implemented as follows: procedure consumer() { while (true) { if (itemCount == 0) { …
curryage
  • 481
  • 1
  • 6
  • 19
0
votes
1 answer

Thread synchronization with multiple objects

I have been facing this problem for many days, Please help me out. I am implementing producer-consumer example using thread synchronization. I have made some twist in this traditional program. Instead of using only one queue object, i have used two…