Producer-consumer patterns of design patterns

Source: Internet
Author: User

I. BACKGROUND

When it comes to producer-consumer models, it is necessary to mention multithreading, multithreaded development is commonly used in development, multi-thread programming is more stable than single-threaded,

A thread that hangs does not affect the normal operation of the entire program. However, dirty reads occur when multiple threads are manipulating a data source at the same time.

Ii. introduction of producer consumer Models

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, but through

Block the queue to communicate, so the producers do not have to wait for consumers to deal with the data, directly throw to the blocking queue, consumers do not find producers to data, and

is taken directly from the blocking queue, the blocking queue is equivalent to a buffer that balances 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 Factory mode

Third party is the factory class, template mode of the third party is the template class. In the process of learning some design patterns, if you first find this model of the third party, can help me

Quickly familiarize themselves with a design pattern.

Third, why use the producer consumer model

In the world of threads, the producer is the thread of production data, and the consumer is the thread of consumption data. In multi-threaded development, if the producer processing speed is very

Fast, and consumer processing is slow, producers must wait for consumers to finish before they can continue to produce data. Similarly, if the consumer's treatment can

Force is greater than the producer, then the consumer must wait for the producer. In order to solve the problem of unbalanced production and consumption capacity, we have a model of producer and consumer.

Four, multi-producer and multi-consumer scenarios

In the multicore era, multithreading concurrency is faster than single-threaded processing, so we can use multiple threads to produce data, as well as multiple

Consumer threads consume data. The more complicated situation is that consumer consumption data may need to continue to be processed, so after the consumer has finished processing the data, it will also

As producers put the data in a new queue and give it to other consumers to continue processing. Such as:

We used this pattern in a long-connected server, and producer 1 was responsible for storing messages sent by all clients in the blocking queue 1, and the consumer 1 from the team

Joins reads the message and then hashes it by the message ID to get one of the n queues, then stores the message in a different queue according to the number, each blocking queue

A thread is allocated 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

Processing.

Five, thread pool and production consumer model

The thread pool class in Java is actually a way of implementing producer and consumer patterns, but I think it's a much smarter way to do it. The producers lost their jobs

To the thread pool, threads pool to create threading and process tasks, if the number of tasks to be run is greater than the number of basic threads in the thread pool, throw the task into the blocking queue, this

It is obviously much smarter than using only one blocking queue to implement producer and consumer patterns, because consumers can deal with them directly, so the speed

Faster, and producers are first saved, and consumers are obviously slower to take this way.

Our system can also use a thread pool to implement multi-producer consumer patterns. such as creating n Java thread pools of different sizes to handle tasks of different natures,

Thread pool 1, for example, reads the data into memory, handing it over to threads in thread pool 2 to continue processing the compressed data. Thread pool 1 primarily handles IO-intensive tasks, thread pool 2

Primarily handles CPU-intensive tasks.

Refer to the following:

http://ifeve.com/producers-and-consumers-mode/

If there are deficiencies and errors, please ask the big guys.

Thank you!!!

2018-10-08 16:49:32

Producer-consumer patterns of design patterns

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.