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.
Read items with multiple consumers - C# Tutorial
From the course: Advanced C#: Thread-Safe Data with Concurrent Collections
Read items with multiple consumers
- [Instructor] There'll be times when you want to have more than one thread consuming the information from the blocking collection. Here's a quick demonstration of that. I have a concurrent queue in my blocking collection here, got one task assigned to produce the items, and that'll do it quickly. Every 50 milliseconds, it'll produce an item. And then I've got two tasks that are calling the consume items method that will remove the items, and they're doing it at a slower pace, 700 milliseconds. So again, I have two worker threads coming in here, and pulling items out of the blocking collection. The code is similar to what we've seen so far. The producers calls complete adding when they've added 20 items, it adds 20 items, I should say. And then a consumer, the only thing I've done here is I do have an if statement that's checking if the producer's done and the blocking collection is empty, and I've also added the thread value…
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)
-