From the course: Advanced C#: Thread-Safe Data with Concurrent Collections
Unlock this course with a free trial
Join today to access over 22,500 courses taught by industry experts.
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
-
-
-
-
-
-
-
-
-
-
(Locked)
Understand the BlockingCollection5m 42s
-
(Locked)
Use the BlockingCollection with ConcurrentQueue3m 2s
-
(Locked)
Use the CompleteAdding method3m 28s
-
(Locked)
Use other collections with BlockingCollection2m 4s
-
(Locked)
Read items with multiple consumers1m 31s
-
(Locked)
Create items with multiple producers2m 48s
-
(Locked)
-