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
1 answer

Searching an algorithm similar to producer-consumer

I would like to ask if someone would have an idea on the best(fastest) algorithm for the following scenario: X processes generate a list of very large files. Each process generates one file at a time Y processes are being notified that a file is…
0
votes
1 answer

Thread synchronization/producer-consumer in java. Printing out numbers 1-10 then 10-1 repeatedly

I'm very new to java and found an exercise to work on basic thread synchronization. The problem is to print out 12345678910 10987654321 repeatedly until the program stops. Ten different threads should be used. This is my code so far: I am first…
0
votes
0 answers

Java producer/consumer threads -- stopping a threads execution via wait

I have two threads in my Android app which are effectively in a producer/consumer relationship; the producer thread (a subclass of Thread) populates a buffer with objects, and the consumer thread (a subclass of AsyncTask) operates on that buffer. …
CCJ
  • 1,619
  • 26
  • 41
0
votes
1 answer

RabbitMQ let consumer receive more tasks before completing earlier task

Based on my understanding of RabbitMQ/AMQP I see an unfortunate tradeoff. The usual workflow is: Producer produces Queue delivers to consumer Consumer acks and does not receive more tasks until the ack Queue considers message processing…
djechlin
  • 59,258
  • 35
  • 162
  • 290
0
votes
1 answer

Client Server Assignment modeled using Java Threads (Producer/Consumer queues)

I have modeled a Client Server application using Java Threads and BlockingQueues (producer and consumer queues) as recommended in a previous question. What I have is server has a BlockingQueue and it will push responses to clients'…
Jiew Meng
  • 84,767
  • 185
  • 495
  • 805
0
votes
1 answer

Compound Producer-Consumer in Java

I have a relationship that must work as follows; thread A publishes some change to thread B, who takes that change and publishes it to thread C. The problem is producer-consumer, and I have no problem using a BlockingQueue to implement it with only…
aquemini
  • 950
  • 2
  • 13
  • 32
0
votes
2 answers

Thread Synchronization with BlockingQueue in Java

I am new to the concept of a BlockingQueue, and was under the impression that it took out the need for wait() and notify(). I have written the following code as a preliminary test of thread synchronization (omitting some code for clarity): q = new…
aquemini
  • 950
  • 2
  • 13
  • 32
0
votes
1 answer

producer and consumer how to exit gracefully?

my task is improving efficiency of making rpc call. I believe I can use producer and consumer example. producer will get response, whereas consumer will write the received object to file. I write a sample program for producer & consumer problem.…
randomness2077
  • 1,119
  • 2
  • 13
  • 25
0
votes
1 answer

Structure of a Multithreaded Flight Control Program in Java

I have to write a program in Java that takes flight data from a CSV file and simulates the flight; that is, use a thread to change the state of each flight. Here's a more specific description from the project page: Write a program that manages…
aquemini
  • 950
  • 2
  • 13
  • 32
0
votes
1 answer

Ruby Multithreaded producer-consumer

I started on Ruby less than a week ago but have already come to appreciate the power of the language. I am trying my hands on a classic producer-consumer problem, implemented as an Orange tree (c.f. http://pine.fm/LearnToProgram/?Chapter=09). The…
Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
0
votes
1 answer

How to get the result in a Producer-Queue-Cosumer Pattern

I've used very much the (Multi)Producer-Queue-Cosumer Design pattern, but I've no idea on how get the result of an operation. I've 3 producer P1, P2, P3, that produce a message IMessage; this message is sent in a syncronized queue and elaborated…
Elvis Dukaj
  • 7,142
  • 12
  • 43
  • 85
0
votes
2 answers

Multiple Producers, Multiple Consumers and Store Problem

I have a scenario where Multiple Producers are running on different machines in a Network. These are Singleton WCF Services which are queuing the Products (Output) in the Central Store which is also a Singleton WCF Service. There are consumers who…
0
votes
1 answer

The right way to create Parallel loop

I have written a code to crunch some data on Azure server. The first time that I wrote the code, it was linear For Each loop and would take forever to be finished, I improved the code by using Parallel as it follows : Parallel.ForEach(users,…
Payam
  • 741
  • 3
  • 14
  • 37
0
votes
3 answers

Multiple producer/consumer and critical section code problem

I am attempting a multiple producer/consumer problem in C, but its not working as expected. The following is some pseudo code to represent my implementation. Thread thread1; Thread thread2; Thread thread3; Data data1; Mutex data1_mutex; Semaphore…
Nippysaurus
  • 20,110
  • 21
  • 77
  • 129
0
votes
1 answer

Finalize vs. IDisposable in BlockingCollection Producer Consumer

I have a simple logger with producer consumer pattern based on BlockingCollection (code is below). public class Logger { public Logger() { _messages = new BlockingCollection(int.MaxValue); _worker = new…
MajesticRa
  • 13,770
  • 12
  • 63
  • 77