Note that there are some explanatory texts on larger screens.

plurals
  1. POAlgorithm for preventing burst from a producer
    text
    copied!<p>I have the following :</p> <ul> <li>One producer that produces random integer (around one every minute). Eg : 154609722148751</li> <li>One consumer that consumes theses integers one by one. Consumption is around 3 seconds long. </li> </ul> <p>From time to time the producer get crazy and produces only one 'kind' of figure very quickly and then get back to normal. Eg : 6666666666666666666666666666666666666666666666675444696 in 1 second.</p> <p>My goal is to have as lower as possible as different kind of figure not consumed. Say, in the previous sample I have :</p> <ul> <li>a lot of '6' not consumed </li> <li>one '7' not consumed </li> <li>one '5' not consumed</li> <li>three '4' not consumed </li> <li>one '9' not consumed</li> </ul> <p>If I use a simple FIFO algorithm I am going to wait a long time before all the '6' being consumed. I would prefer to 'priortize' the other figures and THEN consume the '6'.</p> <p>Does such an algorithm already exists ? (C# implementation is a plus)</p> <p>Currently, I was thinking about this algorithm :</p> <ul> <li>have a queue for each figure (Q0,Q1,Q2 ..., Q9) </li> <li><p>sequentially dequeue one item for each queue :</p> <pre><code>private int _currentQueueId; private Queue&lt;T&gt;[] _allQueues; public T GetNextItemToConsume&lt;T&gt;() { //We assume that there is at least one item to consume, and no lock needed var found = false; while(!found) { var nextQueue = _allQueues[_currentQueueId++ % 10]; if(nextQueue.Count &gt; 0) return nextQueue.DeQueue(); } } </code></pre></li> </ul> <p>Do you have better algorithm than this one ? (or any idea)</p> <p>NB1 : I don't have the lead on the consumption process (that is to say I can't increase the consumption speed nor the number of consumption thread ..) (indeed an infinite consumation speed would solve my issue)</p> <p>NB2 : exact time's figures are not relevant but we can assume that consumption is ten times quicker that production</p> <p>NB3 : I don't have the lead on the 'crazy' behaviour of the producer and in fact it is a normal (but not so frequent) production behaviour </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload