It is possible to use single BlockingCollection
as a buffer between multiple producers and consumers.
In this recipe, we are going to build a Console
application that will create multiple producer tasks which perform an expensive math operation on a small range of numbers. We will also have two consumer tasks that loop through the BlockingCollection
buffer and display the results.
Now, let's take a look at using a single BlockingCollection
with multiple producers and consumers.
Start a new project using the C# Console Application project template and assign
MultiptleProducerConsumer
as the Solution name.Add the following
using
directives to the top of yourProgram
class:using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
Let's start by creating a
static
method on theProgram
class which performs our expensive math operation.private static...