producer-consumer

Synchronized implementation : Java

末鹿安然 提交于 2019-12-11 02:35:30
问题 This is a Consumer-Producer problem in which, I wish to get output as follows: Put: 0 Get: 0 Put: 1 Get: 1 ....and so on. But in contrast to this, the Consumer class consumes same value of q multiple times, inspite of using wait() and notify() methods.. as well as the Producer class overruns the consumer. How can I get synchronized output? This is QFixed class:(which defines put() and get() methods) class QFixed{ int n; boolean valueset = false; synchronized int get(){ if(!valueset){ try {

Can someone explain producer and consumer in P V form?

大兔子大兔子 提交于 2019-12-10 20:58:13
问题 Wikipedia has a sample code that everyone uses. I honestly don't get the P,V thing. It first said The consumer must wait for the producer to produce something if the queue is empty. But then it said Example. A single consumer enters its critical section. Since fullCount is 0, the consumer blocks. I assume blocking means waiting? My homework requires me to understand the use of such binary semaphore and then implement a solution for a different kind of producer-consumer problem. But I don't

Are there race conditions in this producer-consumer implementation?

喜欢而已 提交于 2019-12-10 20:33:35
问题 In section 3.4.1 of Operating System Concepts (Silberschatz, 9th edition), the authors present the producer-consumer problem and give the following implementation that uses a circular buffer (page 125, 126). //Shared variables #define BUFFER SIZE 10 struct Item; Item buffer[BUFFER SIZE]; int in = 0, out = 0; //buffer is empty when in == out //buffer is full when (in + 1) % BUFFER SIZE == out //Producer while (true) { Item next_produced = /*produce item here*/; while (((in + 1) % BUFFER SIZE)

why doesn't a simple python producer/consumer multi-threading program speed up by adding the number of workers?

若如初见. 提交于 2019-12-10 19:19:10
问题 The code below is almost identical to the python official Queue example at http://docs.python.org/2/library/queue.html from Queue import Queue from threading import Thread from time import time import sys num_worker_threads = int(sys.argv[1]) source = xrange(10000) def do_work(item): for i in xrange(100000): pass def worker(): while True: item = q.get() do_work(item) q.task_done() q = Queue() for item in source: q.put(item) start = time() for i in range(num_worker_threads): t = Thread(target

Java Threads Producer Consumer Algorithm not working properly

[亡魂溺海] 提交于 2019-12-10 17:54:17
问题 I am trying to learn threads and hence I wrote a sample producer consumer problem wherein the producer produces numbers from 1 to 10 and the consumer has to display them. But only the consumer displays number 1 and stops. As I said the program is not well written and might be absurd but still I want to figure out the reason why all the numbers from 1 to 10 are not printed as I will remembr best when I code rather than from examples. I am using two varibales to track the completion of the

What's the best way to asynchronously handle low-speed consumer (database) in high performance Java application

自闭症网瘾萝莉.ら 提交于 2019-12-10 14:00:46
问题 One EventHandler (DatabaseConsumer) of the Disruptor calls stored procedures in database, which is so slow that it blocks the Disruptor for some time. Since I need the Disruptor keep running without blocking. I am thinking adding an extra queue so that EventHandler could serve as Producer and another new-created thread could serve as Consumer to handle database's work, which could be asynchronous without affecting the Disruptor Here is some constrain: The object that Disruptor passed to the

Java: Thread producer consumer what is the most efficient way to wait for data to be produced

烈酒焚心 提交于 2019-12-10 04:33:05
问题 When using BlockingQueue to consume data that is produced what is the most efficient method for waiting for the data to appear? Scenario: Step 1) The data list will be a data store where timestamps are added to. These timestamps are required to be ordered by closest to current time priority. This list may be empty. A thread will be inserting the timestamps into it. Produce Step 2) I want to consume the data in here in another thread that will take the timestamps from data and check if they

Using a named mutex to lock a file

二次信任 提交于 2019-12-10 03:03:56
问题 I'm using a named mutex to lock access to a file (with path 'strFilePath') in a construction like this: private void DoSomethingsWithAFile(string strFilePath) { Mutex mutex = new Mutex(false,strFilePath.Replace("\\","")); try { mutex.WaitOne(); //do something with the file.... } catch(Exception ex) { //handle exception } finally { mutex.ReleaseMutex(); } } So, this way the code will only block the thread when the same file is being processed already. Well, I tested this and seemed to work

Rx how to create a sequence from a pub/sub pattern

混江龙づ霸主 提交于 2019-12-09 23:18:20
问题 I'm trying to evaluate using Rx to create a sequence from a pub/sub pattern (i.e. classic observer pattern where next element is published by the producer(s)). This is basically the same as .net events, except we need to generalize it such that having an event is not a requirement, so I'm not able to take advantage of Observable.FromEvent. I've played around with Observable.Create and Observable.Generate and find myself end up having to write code to take care of the pub/sub (i.e. I have to

How to aggregate the data from an async producer and write it to a file?

泄露秘密 提交于 2019-12-09 11:31:04
问题 I'm learning about async/await patterns in C#. Currently I'm trying to solve a problem like this: There is a producer (a hardware device) that generates 1000 packets per second. I need to log this data to a file. The device only has a ReadAsync() method to report a single packet at a time. I need to buffer the packets and write them in the order they are generated to the file, only once a second. Write operation should fail if the write process is not finished in time when the next batch of