Producer/consumer pattern

One of the best patterns to execute long-running operations is the producer/consumer pattern. In this pattern, there are producers and consumers, and one or more producers are connected to one or more consumers through a shared data structure known as BlockingCollection. BlockingCollection is a fixed-sized collection used in parallel programming. If the collection is full, the producers are blocked, and if the collection is empty, no more consumers should be added:

In a real-world example, the producer could be a component reading images from a database and the consumer could be a component that processes that image and saves it into a filesystem:

static void Main(string[] args) 
{ 
  int maxColl = 10; 
  var blockingCollection = new BlockingCollection<int>(maxColl); 
  var taskFactory = new TaskFactory(TaskCreationOptions.LongRunning, 
TaskContinuationOptions.None); Task producer = taskFactory.StartNew(() => { if (blockingCollection.Count <= maxColl) { int imageID = ReadImageFromDB(); blockingCollection.Add(imageID); blockingCollection.CompleteAdding(); } }); Task consumer = taskFactory.StartNew(() => { while (!blockingCollection.IsCompleted) { try { int imageID = blockingCollection.Take(); ProcessImage(imageID); } catch (Exception ex) { //Log exception } } }); Console.Read(); } public static int ReadImageFromDB() { Thread.Sleep(1000); Console.WriteLine("Image is read"); return 1; } public static void ProcessImage(int imageID) { Thread.Sleep(1000); Console.WriteLine("Image is processed"); }

In the preceding example, we initialized the generic BlockingCollection<int> to store the imageID that will be added by the producer and processed through the consumer. We set the maximum size of the collection to 10. Then, we added a Producer item that reads the image from a database and calls the Add method to add the imageID in the blocking collection, which can be further picked up and processed by the consumer. The consumer task just checks any available item in the collection and processes it.

To learn more about the data structures available for parallel programming, please refer to https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/data-structures-for-parallel-programming.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.185.169