Using BlockingCollection<T>
The BlockingCollection<T> class is a thread-safe collection that implements the IProduceConsumerCollection<T> interface. We can add or remove items from the collection concurrently without worrying about synchronization, which is handled automatically. There will be two threads: the producer and the consumer. The producer thread will produce data and we can limit the maximum number of items that can be produced by the producer thread before it enters sleep mode and is then blocked. The consumer thread will consume data and will be blocked when the collection is emptied. The producer thread is unblocked and the consumer thread removes some items from the collection. The consumer thread is unblocked when the producer ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access