Producer Consumer ModelProducer consumer model is a classic model, we all know that in the actual software development, a module is responsible for the production of data, a module responsible for processing data, the production of data modules, image to become a producer, and the processing of data modules, known as consumers.We know that the pattern also requires a buffer between the
in computer science, the producer-consumer problem (also known as the bounded-buffer problem ) is a classical example of a multi-process synchronization problem. the problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer. the producer's job is to generate a piece of data, put it into the buffer and start again. at the same time the consumer is consuming the
Java producer and consumer models are a classic example of Java's lock mechanism, thread security and concurrent programming, I will share with you several different implementations I have encountered.
1. Use the synchronized keyword
Synchronized is used to apply synchronization locks to ensure thread security. Synchronized locks have been greatly optimized since 1.6. In general, synchronized locks are sufficient for synchronization.
Public class pro
[ problem description ]There is a group of producer processes that produce products and provide these products to consumer processes to consume. In order for the producer and consumer processes to execute concurrently, a buffer pool with n buffers is set between them, and the producer process puts all of its manufactured products into a buffer; the consumer pro
When there are multiple threads in a process and it is possible to access the same resource at this point, security issues may occur, so you need to place these resources in a synchronized code block or in a synchronization method before you access them, by synchronized keyword declarations, Only one thread can exist at this time, and the thread cannot manipulate the shared resource until it acquires the corresponding lock, and then the other thread can acquire the lock and then manipulate the s
What is the producer consumer modelSome modules are responsible for production data, which is handled by other modules (the modules here may be: functions, threads, processes, etc.). The module that produces the data is called the producer, and the module that processes the data is called the consumer. The buffer between producer and consumer is called a warehous
Single thread producer#!/usr/bin/envpython#-*-coding:utf-8-*-importrandomimportsysfrom kafkaimportkafkaproducerfromkafka.clientimportlogimporttimeimport json__metaclass__=typeclassproducer:def__init__ (Self, kafkaserver= ' 127.0.0.1 ', kafkaport= ' 9092 ', clientid= "Procucer01", topic= ' Test '): "" for setting up producer configuration information, These configuration items can be found in the source co
Single thread producer#!/usr/bin/envpython#-*-coding:utf-8-*-importrandomimportsysfrom kafkaimportkafkaproducerfromkafka.clientimportlogimporttimeimport json__metaclass__=typeclassproducer:def__init__ (Self, kafkaserver= ' 127.0.0.1 ', kafkaport= ' 9092 ', clientid= "Procucer01", topic= ' Test '): "" for setting up producer configuration information, These configuration items can be found in the source co
Http://www.cnblogs.com/zyp4614/p/6033757.html(Java and operating system process synchronization problem (i) ———— mutex issues)Today is the most classic producer of consumer problems, the simplest version, that is, only one buffer, the buffer can only put one item, that is, regardless of the mutual exclusion relationship.Simple analysis: Producers can put products in buffers when the buffer is empty, consumers can take a product when the buffer is not
Tags: thread producer consumer Mode The producer and consumer modes are shown in. Blog purpose: Use graphs to speak. Sample Code: Package COM. huan; public class produceconsumer {public static void main (string [] ARGs) {middleware = new middleware (); New thread (new producer (middleware )). start (); New thread (new consumer (middleware )). start () ;}}
Producer-consumer is an interesting algorithm. Its existence mainly serves two purposes. The first is to satisfy the producer's constant creation of resources, and the second is to satisfy the consumer's constant demand for resources. Of course, because space is limited, resources cannot be stored infinitely or requested infinitely.
Producer Algorithm
WaitForSingleObject(hEmpty, INFINITE); WaitForSin
Packagedemo;Importjava.util.Properties;ImportKafka.javaapi.producer.Producer;ImportKafka.producer.KeyedMessage;ImportKafka.producer.ProducerConfig; Public classProducer {Private FinalProducerproducer; Public Final StaticString TOPIC = "Test"; Privateproducer () {Properties props=NewProperties (); //The Kafka port is configured here .Props.put ("Metadata.broker.list", "192.168.152.20:9092"); //to configure the serialization class for valueProps.put ("Serializer.class", "Kafka.serializer.StringEn
Public Class Cell{ Bool Flags = False ; Int Result = 0 ; Public Void Write ( Int N){ Lock ( This ){If (Flags){ Try {Monitor. Wait ( This );} Catch (Threadstartexception E){Console. writeline (E );}}Result = N;Console. writeline ( " Write: {0} " , Result );Flags = True ;Monitor. Pulse ( This );}} Public Int Read (){ Lock ( This ){ If (! Flags){ Try {Monitor. Wait ( This );} Catch (Threadstartexception E){Console. writeline (E );}}Console. writeline ( " Read: {0} " , Result );Flags = Fals
This is a creation in
Article, where the information may have evolved or changed.
In the actual business scenario, in order to improve the real-time performance of the system, reduce the pressure of log storage, the need to direct the production of logs to the message middleware, reduce the flume or flumted collection caused by the delay and pressure, this paper realizes the function:Implements a static call to the asynchronous producer AsyncproducerE
1, using the function to implement the producer consumer model(1), the source code is as follows:#!/usr/bin/python#_*_coding:utf-8_*_importthreadingimporttimeimportqueueimport Randomdefproducer (Name,que):whiletrue: ifque.qsize () (2), operation result:650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/89/80/wKioL1gV3Z6TTZnLAACEeavOGY0439.png-wh_500x0-wm_3 -wmp_4-s_3979700276.png "title=" Qq20161030194625.png "alt=" Wkiol1gv3z6ttznlaaceeavogy0
Producer Consumer
Producers create data, control traffic through intermediaries, and deliver it securely to consumers.Applicable environment
The speed of producer production data is inconsistent with the speed with which consumers process data, and intermediaries adjust the data pressure of consumers by caching and blocking.
Sample Example
4 producers produce products, put i
Public class Customer extends thread{ private Queue q; Public Customer (Queue q) { this.q = q; } public void Run () { for (int i = 0; i int value = Q.get (); } } } public class Producer extends thread{ private Queue q; Public Producer (Queue q) { this.q = q; } public void Run () { for (int i = 0; i Q.put (i); } } } public class Queue { int value; Boolean bfull = false; Synchronized public void
In the process, the producer is the thread of production data, and the consumer is the thread of consumption data. In multithreaded development, producers have to wait for the consumer to continue producing data if the producer is processing fast and the consumer processing is slow. Similarly, consumers must wait for producers if their processing power is greater than that of producers. To solve this proble
Design Patterns and concurrent producer-consumer patternsProducer-consumer mode is a classic multithreaded design pattern. It provides a good solution for collaboration between multiple threads.In producer-consumer mode, typically consists of two types of threads, that is, several producer threads and a number of consumer threads.
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.