I was reading Dataflow (Task Parallel Library), and there is a portion which says:
When you specify a maximum degree of parallelism that is larger than 1, multiple messages are processed simultaneously, and therefore, messages might not be processed in the order in which they are received. The order in which the messages are output from the block will, however, be correctly ordered.
What does it means?
Example, I set my action block with degree of parallelism = 5:
testActionBlock = new ActionBlock<int>(i => Consumer(i),
new ExecutionDataflowBlockOptions()
{
MaxDegreeOfParallelism = 5
});
await Producer();
testActionBlock.Completion.Wait();
My Producer() basically queue numbers into the block:
private async Task Producer()
{
for (int i=0; i<= 1000; i++)
{
await testActionBlock.SendAsync(i);
}
testActionBlock.Complete();
}
And my Consumer(i) just write out the lines:
private async Task Consumer(int i)
{
if (i == 1)
{
await Task.Delay(5000);
}
Console.WriteLine(i);
}
Does it means that Consumer(2) will be blocked until Consumer(1) has finished processing (since there is a 5 sec delay)? I tested out the code and it doesn't seems to be the case. Even when I removed the 5 sec delay, I don't see the output to be in order.
[Update]
bBlock = new BufferBlock<int>(option);
testActionBlock = new ActionBlock<int>(i => Consumer(i),
new ExecutionDataflowBlockOptions()
{
MaxDegreeOfParallelism = 5
});
bBlock.LinkTo(testActionBlock);
await Producer();
testActionBlock.Completion.Wait();
My Producer() now will add to the bBlock:
private async Task Producer()
{
for (int i=0; i<= 1000; i++)
{
await bBlock.SendAsync(i);
}
bBlock.Complete();
}
So, in this case, Consumer(1) will await for 5 sec, before Consumer(2) can proceed?