From the course: Advanced C#: Thread-Safe Data with Concurrent Collections

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Use other collections with BlockingCollection

Use other collections with BlockingCollection - C# Tutorial

From the course: Advanced C#: Thread-Safe Data with Concurrent Collections

Use other collections with BlockingCollection

- [Narrator] By default, when you instantiate a blocking collection, it will use a concurrent queue as the data store. But you can be explicit about this, and pick your own data type, any type that implements the correct interface. So what I've done here on line 29 is show you the syntax. So I instantiate the blocking collection, then hold an integer value, and then in the constructor, I pass on a parameter, which is a new concurrent queue of int. This is the explicit way of setting it to concurrent queue. Then I have these two tasks, one that produces the items, and one that consumes the items. The code is familiar, here's the produce items, so I have the same wow loop, and then the condition is here on line 73: if the counter is greater than or equal to 8, then I call complete heading. So we'll add 8 items to the blocking collection, and then the consumer will remove 8 items, and then what'll I do next? Well next I…

Contents