One or two aspects of application concurrency in project development: Project Development concurrency
In a multi-threaded environment, BlockingCollection and ConcurrentQueue are used to consume resources produced by producers. This is my own practice of multiple producers and consumers, it is also identified based on IsComplete of the blocking queue under a single task.
Blocking queues are simpler, but internal consumer threads are more suitable for using separate threads. They are not suitable for using thread pools. When blocking queues are empty, the consumer threads are blocked, of course, it is not recommended that the threads in the thread pool be blocked, and the performance of the blocked queue is not as high as that of the ConcurrentQueue.
I have encountered multiple producers and consumers in the project. There is no problem with multiple producers. But how to consume the resources of the producers under multiple threads is troublesome. It cannot be done simply by determining the quantity, I also found some resources on the Internet, but I also gave a demo, not all of them. I thought of a solution for the time being. I went back and studied the Thread-based approach encapsulated by others. In fact, it is based on the <. NET blocking queue BlockingCollection's correct opening method>, and there is nothing, but this is a good idea. In the future, try to encapsulate the thread flag by yourself, instead of relying on the FCL blocking queue. The code is as follows:
ConcurrentDictionary <string, string> dic1 = new ConcurrentDictionary <string, string> (); ConcurrentDictionary <string, string> dic2 = new ConcurrentDictionary <string, string> (); concurrentQueue <string> queue = new ConcurrentQueue <string> (); BlockingCollection <string> blockingCollection = new BlockingCollection <string> (); var t = new Task [50]; Console. writeLine ("the producer starts writing data ............. \ r \ n "); for (int I = 0; I <= 49; I ++) {t [I] = Task. factory. startNew (param) => {Console. writeLine ("The *** blocking queue input in the producer: {0}", param. toString (); blockingCollection. add (param. toString (); Console. writeLine ("the number of blocked queues in the producer *** is: {0}", blockingCollection. count); Console. writeLine ("*** dictionary dic1 input in the producer: {0}", param. toString (); dic1.TryAdd (param. toString (), param. toString (); Console. writeLine ("the number of dictionary dic1 in the *** producer is: {0}", dic1.Cou Nt); Console. writeLine ("*** dictionary dic2 input in the producer: {0}", param. toString (); dic2.TryAdd (param. toString (), param. toString (); Console. writeLine ("the number of dictionary dic2 in the *** producer is: {0}", dic2.Count); Console. writeLine ("** queue input in the producer: {0}", param. toString (); queue. enqueue (param. toString (); Console. writeLine ("Number of *** queues in the producer: {0}", queue. count) ;}, I) ;}// Thread. sleep (1, 500); Console. writeLine ("\ r \ n the consumer starts to read data ............. \ r \ N "); while (! BlockingCollection. isCompleted) {Task tt = Task. factory. startNew () => {foreach (var B in blockingCollection. getConsumingEnumerable () {Console. writeLine ("the number of dictionary dic1 in the consumer *** is: {0}", dic1.Count); Console. writeLine ("the number of dictionary dic2 in the consumer *** is: {0}", dic2.Count); Console. writeLine ("the number of blocked queues in the consumer *** is: {0}", blockingCollection. count); string value1 = ""; string value2 = ""; dic1.TryGetValue (B, out value1); dic2.TryGetValue (B, out value2); Console. writeLine ("the value of the key value {0} of the *** dictionary dic1 in the consumer is: {1}", B, value1); Console. writeLine ("the value of the key value {0} of the *** dictionary dic1 in the consumer is: {1}", B, value2); Console. writeLine ("the number of queues in the consumer *** is: {0}", queue. count); Console. writeLine ("the number of dictionaries in the consumer *** is: {0}", dic1.Count); if (queue. count = 50) {blockingCollection. completeAdding () ;}});} Console. writeLine ("whether to add: {0}", blockingCollection. isCompleted );
Refer:
Parallel Programming in. Net-7. High-Performance asynchronous Queue Based on BlockingCollection