Exercise: Producer-Consumer using BlockingCollection

In a previous exercise you made a Producer-Consumer framework with a BoundedBuffer (and an UnboundedBuffer).

In this exercise you must make another version of the Producer-Consumer framework, now using a BlockingCollection.

Depending on how you use BlockingCollection it can be bounded or unbounded.

Getting ready

Create a new Solution in Visual Studio. Name the Solution "ProducerConsumerBlockingCollection".

Producer

Create a class Producer.

The constructor must have 2 parameters

void Run() method

Consumer

Create a class Consumer.

The constructor must have 1 parameter

void Run() method

Main

  1. 1 buffer + 1 producer + 1 consumer. Use Parallel.Invoke(...)
  2. 1 buffer + 1 producer + 2 consumers. Use Parallel.Invoke(...)
  3. 1 buffer using either ConcurrentBag or ConcurrentStask as a constructor parameter + 1 producer + 1 consumer

MiddleMan

Create a class MiddleMan. The MiddleMan takes an element from one buffer and adds the element to another buffer.

The constructor must have 2 parameters

void Run() method

Main

Extra: Logging

Substitute all Console.WriteLine(...) with logging.s