Java simulated producer consumer issues
Java simulated producer consumer issues
I. Syncronized
To solve the problem of producer consumption, first take a look at the syncronized keyword in Java.
The synchronized keyword is used to
; ImportKafka.producer.ProducerConfig; /** Own in the source server writes the producer to Kafka inserts the data, note the file "Producer.properties puts under the Linux the jar file same directory * listens to a directory the file data then writes Kafka * Nohup Java-jar Portallog_producer.jar portallog/var/apache/log
Introduction
The producer and consumer problems are classic issues in the thread model:Same time periodIntranet sharingSame bucketAs shown in, if the producer stores data into the space and the consumer uses the data, the following situations may occur if the data is not coordinated:
Producer consumer Diagram
The storage space is full, and the
Wait (), notify (), and Notifyall () are all methods of the Java base class Java.lang.Object.Popular explanationsWait (): Waits for another thread to wake up on the current thread.Notify (): Wakes up a thread that is waiting for this object's monitor.Notifyall (): Wakes up all the threads waiting on this object monitor.These three methods are the underlying mechanism provided by the Java language to impleme
(container, Producermonitor, consumermonitor)); Thread C2=NewThread (NewConsumer (container, Producermonitor, consumermonitor)); P1.start (); P2.start (); C1.start (); C2.start (); }}At present, the first to write, the temporary operation is not a problem, if there is a problem, please crossing pointed out.Areas to be improved:1. Container there is no better way to implement2. Container can have multiple, so that you can randomly find a usable container assigned to
1 // The standard idiom for calling the wait 2 synchronized (sharedobject) {3 while (condition) {4 sharedobject.wait (); releases lock, and reacquires on Wake up5 }6 // do action based upon condition e.g. take or put to queue 7 }Canonical code template using the wait and notify functions.The purpose of using wait in the while loop is to check whether the condition is satisfied before and after the thread is awakened, and if the condition is not changed,
(); }Because after another thread notifyall, after waking this thread, it is impossible to confirm that the test condition must be met at this time. There is no problem with two threads, but there will be problems with more threads, because you cannot confirm that you are being awakened by the producer thread, and that there may be other threads that have changed the state variable before waking (this is the message in the drop), so there will
the consumer first grab, at this time call get () method namely consume take away, but at this time flag is true, therefore will enter wait () method, so at this time can only be producer preemption to CPU, according to producer Call Set () method, Post-production execution Flag=false; Notify ();At this time the producers and consumers in the two threads and stand on the same starting line, c
What is the Producer/consumer model?A module is responsible for generating the data, which is handled by another module (the module here is generalized, which can be classes, functions, threads, processes, etc.). The module that produces the data is visually called the producer, and the module that processes the data is called the Consumer. Between producers and consumers in the addition of a buffer zone, o
no oh, there is waiting to be consumed, there is no production data ready to be consumed. - if(s.flag) { the Try { - s.wait (); -}Catch(interruptedexception e) { - //TODO auto-generated Catch block + e.printstacktrace (); - } + } A at //once the flag is marked False, execute the following code - - if(x% 2 = = 0) { -S.name = "
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 suf
Package com.cn.test3; Java multithreaded simulation producer consumer issues//producerconsumer is the main class, producer producers, consumer consumers, product products//storage WarehouseComments: I wrote the output below the program, you can look at it, in fact very easy, you imagine the product from production, to take out a production line, we define two thr
1 Producer-consumer PatternProducer-consumer pattern is mainly to create a "bridge participant" between producer and consumer, to solve the mismatch between producer thread and consumer thread speed.When you want to transfer data from a thread produccer a participant to another thread consumer the participant, you can add a channel participant in the middle, stor
Java: producer and consumer issues
I remember the first time I made a java question, I saw "Writing producer and consumer problems" and thought it was related to the factory model. Now, I think it's a crash.Java producer and consumer issues are actually about multi-thread co
Java thread synchronization usually requires the use of sychronized to lock critical resources. The so-called critical resources are the resources used by these threads.
Sychronized is usually placed before the method name, which indicates that the method is synchronized, and is actually locking this.
Alternatively, you can lock an object that is commonly used before an object.
The examples of producers and consumers are very classic. here we need to
Producer, consumer, fixed-length buffer, and thread can be interrupted externally
Import java. util. arrays;Import java. util. date;Import java. util. Collections list;Import javax. Swing .*;
Public class producerconsumer {
Public static jframe inst;
Public static void main (string [] ARGs ){
Bufferlock buffer = new bu
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.