Using BlockingCollection
BlockingCollection<T>
is one of the most useful concurrent collections. As we saw in Chapter 7, BlockingCollection<T>
was created to be an implementation of the producer/consumer pattern for .NET. Let’s review some of the specifics of this collection before creating a different kind of sample project.
BlockingCollection details
One of the major draws of BlockingCollection<T>
for developers working with parallel code implementations is that it can be swapped to replace List<T>
without too many additional modifications. You can use the Add()
method for both. The difference with BlockingCollection<T>
is that calling Add()
to add an item will block the current thread if another read or write operation is in process. If you want to specify a timeout period on the operation, you can use TryAdd()
. The TryAdd()
method optionally supports both timeouts and cancellation tokens.
Removing items from BlockingCollection<T...