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.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.