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.

Work with ConcurrentQueue

Work with ConcurrentQueue

- [Instructor] We're working with the ConcurrentQueue in this demonstration. Code here is in this Demo method. I start by creating an instance of ConcurrentQueue of type robot. This is similar to a demo we looked at earlier in the course, and where I was adding items to the queue. And in that example, when I had two threads, adding items to the queue, we would occasionally see an array exception. And that won't happen in this example, because we're using ConcurrentQueue. What I've got is two methods that set up teams, one is being run by task one, the other by task two, and they're both going to run in separate threads. That code is down here. Task one is going to run this code. And the first robot that it'll put in the queue is Robot10. And then 11, 12, 13 and 14. So they'll always be in this order. So the first item to come out of the queue will be Robot10. That's not exactly true, because we have another…

Contents