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

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