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

Unlock this course with a free trial

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

Create items with multiple producers

Create items with multiple producers

- In this scenario, we'll look at having two producers adding items to blocking collection and one consumer. And to make this more readable, I modified the produce items method and added two parameters, one called item count and one called start number. So the first thread will add six items starting at 10. The second thread will add seven items starting at 20. The majority of the code in produce items and consumed items is the same as the previous demo. So let's take a look at what it looks like when we run the application. I get pathway through the process, and an exception is thrown, and it's caught in Visual Studio and it's an invalid operation exception. The tech says the collection has been marked as complete with regards to additions. What does that mean? Remember that we have two threads calling produce items and one of those threads when it's done adding all of its items. We'll call complete adding. And…

Contents