Producer/consumer model 1: Overview

Source: Internet
Author: User
Tags mail example

This article turned from programming blog: http://program-think.blogspot.com/2009/03/producer-consumer-pattern-0-overview.html

 

Today we will introduce the "producer/consumer model", which can be used in many development fields. Some may have been confused: I have never heard of this in the 23 Gang of Four models! In fact, the 23 classic gof models are mainly based on OO (from the title of design patterns: Elements of reusable object-oriented software ). The pattern can be oo or non-oo pattern.

 

Introduction
Let's get down to the truth! In the actual software development process, we often encounter the following scenarios: a module is responsible for generating data, and the data is processed by another module (ModuleIn a broad sense, it can be classes, functions, threads, processes, etc ). The module that generates data is visually calledProducerThe data processing module is calledConsumer.

 

Abstract producers and consumers alone is not enough to be a producer/consumer model. This mode also requires a buffer between the producer and consumer as a mediation. ProducerThe data is put into the buffer, while the consumer extracts the data from the buffer. The approximate structure is shown in.

 

 

In order not to be too abstract, let's give an example of sending a mail (although it is no longer a good time to send a mail, this example is more appropriate ). Suppose you want to send a letter, the general process is as follows:

1. You write the letter-equivalent to the producer's data
2. You put the mail in the mailbox-it is equivalent for the producer to buffer the data
3. The Postman extracts the letter from the mailbox-equivalent to the consumer taking the data out of the buffer zone
4. The postman takes the mail to the post office for corresponding processing-equivalent to the number of consumer Processing

 

Advantages
Some colleagues may ask: what is the use of this buffer zone? Why not let the producer directly call a function of the consumer and pass the data directly? What can we do with such a buffer? In fact, this is very exquisite, and it has the following benefits.

 

◇ Decoupling
Assume that the producer and consumer are two classes. If the producer directly calls a method of the consumer, the producer will depend on the consumer (that is, coupling ). In the futureCodeA change may affect the producer. If both of them depend on a buffer zone, the coupling between the two is reduced accordingly.

 

In the preceding example, if you do not use a mailbox (that is, a buffer zone), you must directly send the mail to the postman. Some people may say that it is quite easy to directly give the postman? In fact, it is not easy. You must know who is a postman to give the mail to him (just wear the uniform on your body. If someone is fake, it will be miserable ). This produces the dependency between you and the postman (equivalent to that of the producer and consumer)Strong Coupling). In case the postman changes people one day, You Need To Know it again (equivalent to modifying the producer code due to consumer changes ). The mailbox is relatively fixed compared with the postman, and the cost of relying on it is relatively low (equivalent toWeak Coupling).

 

◇ Concurrency)
Another drawback is that a producer directly calls a method of a consumer. Because function calls are synchronous (or blocked), the producer has to wait until the consumer's method is returned. In case the consumer processes data slowly, the producer will waste a good time.

 

After the producer/consumer mode is used, the producer and consumer can be two independent concurrent subjects (Common concurrency types include process and thread, the following post will talk about applications under two concurrent types ). The producer can output the next data as soon as the data is lost to the buffer zone. Basically, there is no need to rely on the processing speed of the consumer.

 

In fact, this mode was mainly used to handle concurrency issues.

 

From the mail example. If there is no mailbox, you have to stand at the intersection with a letter and wait for the postman to come over (equivalent to the producer blocking); or the postman has to ask door-to-door who wants to send a mail (equivalent to consumer polling ). No matter which method is used, it's pretty good.

 

◇ Support for idle and busy periods
The buffer has another benefit. If the speed of data manufacturing is fast and slow, the benefits of the buffer zone will be reflected. When data is created quickly, the consumer cannot process the data. unprocessed data can be temporarily stored in the buffer zone. When the producer's manufacturing speed slows down, the consumer will slowly process it.

 

In order to fully reuse and reduce typing, let's talk about the mail example. Assume that the postman can only take away 1000 messages at a time. If a greeting card is sent on Valentine's Day (or Christmas), more than 1000 emails need to be sent. At this time, the mailbox buffer zone will be used. The postman saves the delayed emails in the inbox and takes them again when they arrive.

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.