producer-consumer

Blocking queue and multi-threaded consumer, how to know when to stop

杀马特。学长 韩版系。学妹 提交于 2019-11-27 16:58:10
I have a single thread producer which creates some task objects which are then added into an ArrayBlockingQueue (which is of fixed size). I also start a multi-threaded consumer. This is build as a fixed thread pool ( Executors.newFixedThreadPool(threadCount); ). I then submit some ConsumerWorker intances to this threadPool, each ConsumerWorker having a refference to the above mentioned ArrayBlockingQueue instance. Each such Worker will do a take() on the queue and deal with the task. My issue is, what's the best way to have a Worker know when there won't be any more work to be done. In other

RabbitMQ C# API Event based Message Consumption

强颜欢笑 提交于 2019-11-27 16:20:51
问题 while (true) { BasicDeliverEventArgs e = (BasicDeliverEventArgs)Consumer.Queue.Dequeue(); IBasicProperties properties = e.BasicProperties; byte[] body = e.Body; Console.WriteLine("Recieved Message : " + Encoding.UTF8.GetString(body)); ch.BasicAck(e.DeliveryTag, false); } This is what we do when we Retrieve Message by subscription..We use While Loop because we want Consumer to listen Continously..what if i want to make this even based..that is when a new message arrives in the queue at that

Multiple producers, single consumer

耗尽温柔 提交于 2019-11-27 14:27:11
I have to Develop a multithreaded application, where there will be multiple thread,each thread generates custom event log and need to saved in saved in queue (not microsoft MSMQ). There will be another thread which read log data from queue and manipulate it with certain information to save log information in to a file.Basically here we are implementing multiple producer single Consumer paradigm. Can any body Suggest me how to implement this in C++ or C# . Thanks, This kind of thing is very easy to do using the BlockingCollection<T> defined in System.Collections.Concurrent . Basically, you

C# Producer/Consumer pattern

試著忘記壹切 提交于 2019-11-27 14:22:05
问题 I have simple one-producer/two-consumers code as follows but the output shows that only C2 is consuming. Are there any bugs in my code? class Program { static void Main(string[] args) { Object lockObj = new object(); Queue<string> queue = new Queue<string>(); Producer p = new Producer(queue, lockObj); Consumer c1 = new Consumer(queue, lockObj, "c1"); Consumer c2 = new Consumer(queue, lockObj, "c2"); Thread t1 = new Thread(c1.consume); Thread t2 = new Thread(c2.consume); t1.Start(); t2.Start()

producer/consumer work queues

痴心易碎 提交于 2019-11-27 00:58:58
问题 I'm wrestling with the best way to implement my processing pipeline. My producers feed work to a BlockingQueue. On the consumer side, I poll the queue, wrap what I get in a Runnable task, and submit it to an ExecutorService. while (!isStopping()) { String work = workQueue.poll(1000L, TimeUnit.MILLISECONDS); if (work == null) { break; } executorService.execute(new Worker(work)); // needs to block if no threads! } This is not ideal; the ExecutorService has its own queue, of course, so what's

Implementing the Producer/Consumer Pattern in C#

瘦欲@ 提交于 2019-11-26 22:45:52
How can I implement the Producer/Consumer patterns in C# using Events and Delegates ? What do I need to keep an eye out for when it comes to resources when using these design patterns? Are there any edge cases I need to be aware of? I know this thread is quite a bit old, but since I came across it sometimes in my searches, I decided to share this producer-consumer code for people wondering how to implement a simple generic producer-consumer job queue. The Job class is used to 'store' an object's method call in the form of a delegate. The delegate is then called when the job is processed. Any

Job queue as SQL table with multiple consumers (PostgreSQL)

陌路散爱 提交于 2019-11-26 22:32:36
问题 I have a typical producer-consumer problem: Multiple producer applications write job requests to a job-table on a PostgreSQL database. The job requests have a state field that starts contains QUEUED on creation. There are multiple consumer applications that are notified by a rule when a producer inserts a new record: CREATE OR REPLACE RULE "jobrecord.added" AS ON INSERT TO jobrecord DO NOTIFY "jobrecordAdded"; They will try to reserve a new record by setting its state to RESERVED. Of course,

Blocking queue and multi-threaded consumer, how to know when to stop

冷暖自知 提交于 2019-11-26 22:30:18
问题 I have a single thread producer which creates some task objects which are then added into an ArrayBlockingQueue (which is of fixed size). I also start a multi-threaded consumer. This is build as a fixed thread pool ( Executors.newFixedThreadPool(threadCount); ). I then submit some ConsumerWorker intances to this threadPool, each ConsumerWorker having a refference to the above mentioned ArrayBlockingQueue instance. Each such Worker will do a take() on the queue and deal with the task. My issue

Wait and notify in Consumer and Producer Threads

非 Y 不嫁゛ 提交于 2019-11-26 17:25:22
问题 Just started learning multi-threading. I have 5 producers and 2 consumers in multiple threads. Basically this program adds 100 items into the queue. The producer will stop adding when the queue size is 100. I would like the consumer to notify the producer when the consumer removes all the items from the queue so that the producer can start adding again. Currently the producer will wait but never gets notified by the consumer. Producer: public class Producer implements Runnable { private

Implementing the Producer/Consumer Pattern in C#

白昼怎懂夜的黑 提交于 2019-11-26 08:26:16
问题 How can I implement the Producer/Consumer patterns in C# using Events and Delegates ? What do I need to keep an eye out for when it comes to resources when using these design patterns? Are there any edge cases I need to be aware of? 回答1: I know this thread is quite a bit old, but since I came across it sometimes in my searches, I decided to share this producer-consumer code for people wondering how to implement a simple generic producer-consumer job queue. The Job class is used to 'store' an