Demo of producer and consumer Application
Previous articles <. the correct way to open the blocking queue BlockingCollection in. NET> <two things about concurrent processing of applications in project development> from the perspective of code and theory, fully utilizes the attribute IsComplete and CompleteAdding of BlockingCollection provided by Microsoft to complete the concurrent design. This time, we will Design Concurrency from a streamlined producer consumer alone. For more information about the demo, see <. net parallel programming-4. implement High-Performance asynchronous queue >><. net parallel programming-7. high-performance asynchronous Queue Based on BlockingCollection>. For the code design logic, see:
Figure 1 shows the concurrency mode of the blocked queue, and Figure 2 shows the concurrency mode of the asynchronous queue.
According to the code above, the implementation logic of asynchronous queues is different from the implementation logic of blocking queues, but in fact, asynchronous queues are consumed by multiple consumers, in recursion, tasks are constantly called to open new threads, and the number of consumer threads depends on the number of queues. This is not very good. Of course we can make some improvements, blocking queues are consumed by a single consumer. We can easily change to multiple consumers. In general, this design is still very good. Take a note and study it later. It will be used directly in the project and the next spring moves!