Creating a Blocking Collection instance

The first step in the listing is creating the BlockingCollection, which I have done using the default constructor. The class is strongly typed, so I have had to declare the type that will be in the collection, in this case Deposit.

// create the blocking collection

BlockingCollection<Deposit> blockingCollection = new BlockingCollection<Deposit>();

The default constructor creates unbounded collections, which means that there is no limit to the number of outstanding work items in the collection. The producers can put items in the collection at a much faster rate than the consumers are taking them out again, but the collection will continue to accept new items and let the backlog grow.

You create a bounded collection by using a constructor version that takes an int argument, as in the following statement:

BlockingCollection<Deposit> blockingCollection = new BlockingCollection<Deposit>(5);

This creates an instance of BlockingCollection that allows up to five items to be in the collection at any given time. If a producer tries to add an item when there are more than five items, the Add() method will block until a consumer has removed an item using the Take() mTake() method.

0 0

Post a comment