producer-consumer

Data type for a “closable” queue to handle a stream of items for multiple producers and consumers

做~自己de王妃 提交于 2021-01-27 13:38:56
问题 Is there a specific type of Queue that is "closable", and is suitable for when there are multiple producers, consumers, and the data comes from a stream (so its not known when it will end)? I've been unable to find a queue that implements this sort of behavior, or a name for one, but it seems like a integral type for producer-consumer type problems. As an example, ideally I could write code where (1) each producer would tell the queue when it was done, (2) consumers would blindly call a

Java BlockingQueue with batching?

♀尐吖头ヾ 提交于 2020-06-22 09:36:28
问题 I am interested in a data structure identical to the Java BlockingQueue, with the exception that it must be able to batch objects in the queue. In other words, I would like the producer to be able to put objects into the queue, but have the consumer block on take() untill the queue reaches a certain size (the batch size). Then, once the queue has reached the batch size, the producer must block on put() untill the consumer has consumed all of the elements in the queue (in which case the

Java BlockingQueue with batching?

旧城冷巷雨未停 提交于 2020-06-22 09:36:12
问题 I am interested in a data structure identical to the Java BlockingQueue, with the exception that it must be able to batch objects in the queue. In other words, I would like the producer to be able to put objects into the queue, but have the consumer block on take() untill the queue reaches a certain size (the batch size). Then, once the queue has reached the batch size, the producer must block on put() untill the consumer has consumed all of the elements in the queue (in which case the

Reading a text file in the Producer paradigm

社会主义新天地 提交于 2020-04-18 05:48:40
问题 There is a task to read a text file in a producer paradigm. The producer interface is defined as the following: public interface Producer<ITEM> { /** * Produces the next item. * * @return produced item */ ITEM next(); /** * Tells if there are more items available. * * @return true if there are more items, false otherwise */ boolean hasNext(); } Current code to read the text file is: public static void readTextFile(File file, Charset charset, Consumer<String> consumer) { try (InputStreamReader

How to process data from a file in parallel in several threads and write them into another file while keeping the original data order (C#)

北城以北 提交于 2020-03-23 09:02:20
问题 I would like to ask you rather a general question (even though I'm rather interested in how to achieve it in C#). I have a huge file which I want to read by chunks, process the chunks somehow in parallel in several threads to make the processing faster and then write the processed data to another file in the same order as the original data chunks were read (i.e. making sure that the first data chunk read from the input file will be processed and saved first in the output file, the second

C++ Producer Consumer stuck in deadlock

余生颓废 提交于 2020-02-06 15:45:29
问题 I'm trying to create a producer-consumer program, where the consumers must keep running until all the producers are finished, then consume what's left in the queue (if there's anything left) and then end. You can check my code bellow, I think I know where the problem (probably deadlock) is, but I don't know how to make it work properly. #include<iostream> #include<cstdlib> #include <queue> #include <thread> #include <mutex> #include <condition_variable> using namespace std; class Company{

Call the method of Java class which implements runnable after creating its thread object

旧巷老猫 提交于 2020-01-24 12:17:35
问题 I have a java class SomeClass implements Runnable Which has a method display(). When I create a thread of this class Thread thread1 = new Thread(new SomeClass()); Now how I can call the display() method using the thread instance? 回答1: You will end up calling start() on thread1 . SomeClass will override run() method which in turn need to call display() method. This way when you call start() , run method of SomeClass() object will be invoked and display() method will be executed. Example:

Apache Camel: Polling consumer

♀尐吖头ヾ 提交于 2020-01-23 17:52:05
问题 I'm new to Apache Camel and I'm trying to understand and use the Polling Consumer EIP in a simple project but I feel a little bit lost.. Could someone please help me with a little explanation or even with a little working example. Any help would be appreciated Thanks in advance 回答1: for most use cases, you can create a consumer by just defining them in the from() clause in a route... from("activemq:inbox").to(new MyProcessor()); but, you can also write your own POJO polling consumer logic for

Producer/ Consumer pattern using threads and EventWaitHandle

久未见 提交于 2020-01-23 01:22:08
问题 I guess it is sort of a code review, but here is my implementation of the producer / consumer pattern. What I would like to know is would there be a case in which the while loops in the ReceivingThread() or SendingThread() methods might stop executing. Please note that EnqueueSend(DataSendEnqeueInfo info) is called from multiple different threads and I probably can't use tasks here since I definitely have to consume commands in a separate thread. private Thread mReceivingThread; private