Using producer and consumer patterns in concurrent programming can solve most concurrency problems. This mode improves the overall processing speed of the program by balancing the productivity of the production line and the consuming thread.
Using producer and consumer patterns in concurrent programming can solve most concurrency problems. This mode improves the overall processing speed of the program by balancing the productivity of the production line and the consuming thread.
Using producer and consumer patterns in concurrent programming can solve most concurrency problems. This mode improves the overall processing speed of the program by balancing the productivity of the production line and the consuming thread.
What is the producer consumer model
The producer-consumer model solves the problem of strong coupling between producers and consumers through a container. Producers and consumers do not communicate with each other directly, and through the blocking queue to communicate, so producers do not have to wait for consumer processing after the production of data, directly to the blocking queue, consumers do not find producers to data, but directly from the blocking queue, the blocking queue is equivalent to a buffer, Balance the processing power of producers and consumers.
What is the producer consumer model
The producer-consumer model solves the problem of strong coupling between producers and consumers through a container. Producers and consumers do not communicate with each other directly, and through the blocking queue to communicate, so producers do not have to wait for consumer processing after the production of data, directly to the blocking queue, consumers do not find producers to data, but directly from the blocking queue, the blocking queue is equivalent to a buffer, Balance the processing power of producers and consumers.
This blocking queue is used to decouple producers and consumers. Throughout most design patterns, a third party is found to decouple, such as the factory model of the third party is the factory class, template mode of the third party is a template class. In the process of learning some design patterns, if we first find the third party of this pattern, we can quickly familiarize ourselves with a design pattern.
This blocking queue is used to decouple producers and consumers. Throughout most design patterns, a third party is found to decouple, such as the factory model of the third party is the factory class, template mode of the third party is a template class. In the process of learning some design patterns, if we first find the third party of this pattern, we can quickly familiarize ourselves with a design pattern.
In the multicore era, multithreading concurrency is faster than single-threaded processing, so we can use multiple threads to produce data, as well as consume data using multiple consumer lines. What is more complicated is that consumer consumption data may need to continue to be processed, so that after the consumer has processed the data, it will also be the producer to put the data in the new queue, to other consumers to continue to deal with. Such as:
We use this mode in a long-connected server, the producer 1 is responsible for all the client sent messages in the blocking queue 1, the consumer 1 read the message from the queue, and then hash through the message ID to get one of the n queue, and then according to the number of messages stored in a different queue, Each blocking queue allocates a thread to consume the data in the blocking queue. If consumer 2 is unable to consume the message, the message is then thrown back into the blocking queue 1 and handed over to other consumers for processing
Using producer and consumer patterns in concurrent programming can solve most concurrency problems. This mode improves the overall processing speed of the program by balancing the productivity of the production line and the consuming thread.