Thread collaboration-Producer/consumer issues

Source: Internet
Author: User

Producer/consumer issues are a classic example of thread synchronization and communication. This problem describes two threads that share fixed-size buffers, which are problems that the so-called "producer" and "consumer" can actually run. The primary role of the producer is to generate a certain amount of data into the buffer, and then repeat the process. At the same time, consumers consume the data in buffers. The key to this issue is to ensure that producers do not add data when the buffer is full, and consumers do not consume data when the buffer is empty. To solve this problem, you must let the producer hibernate when the buffer is full (or simply discard the data), and wait until the next time the consumer consumes the data in the buffer, the producer can wake up and start adding data to the buffer. It is also possible for consumers to hibernate when the buffer is empty, wait until the producer adds data to the buffer, and then wake the consumer, usually using the method of inter-thread communication to resolve the problem. If the workaround is not perfect, the deadlock situation is prone to occur. When a deadlock occurs, two threads fall into hibernation, waiting for the other person to wake up. The problem can also be extended to multiple producers and consumers.

Suppose there is a case, there is a plate, there is only one egg on the plate, a thread dedicated to the plate eggs, if there are eggs on the plate, then wait until the plate is no eggs, B thread specifically from the plate to take the eggs, if there is no eggs on the plate, then wait until the plate has eggs. Here the plate is a mutually exclusive area, each egg is mutually exclusive, each egg is mutually exclusive, a line Cheng eggs, if the B thread to take eggs, because a does not release the lock, b thread is waiting state, into the blocking queue, put eggs, to notify the B thread to take eggs, B thread into the ready queue, in turn, b thread take eggs, if a thread to put eggs, because B thread does not release the lock, a thread is in wait state, into the blocking queue, after the eggs, to notify a line Cheng eggs, a thread into the ready queue. We hope that when there is an egg on the plate, a thread is blocked, b thread is ready, there is no egg on the plate, a thread is ready, B thread is blocked, the code is as follows:

Plate class. Can put, take eggs public class Plate {list<object> eggs=new arraylist<object> ();//egg plate//take egg public synchronized Object Getegg () {while (Eggs.size () ==0) {try {wait ();} catch (Interruptedexception e) {e.printstacktrace ();}} Object egg=eggs.get (0); Eggs.clear ();//emptying the tray notify ();//Wake up a thread-ready queue System.out.println ("Get the eggs") of the blocking queue; return egg; Put egg public synchronized void Putegg (Object egg) {while (Eggs.size () >0) {try {wait ()} catch (Interruptedexception e) { E.printstacktrace ();}} Eggs.add (egg);//Put an egg notify ();//Wake up a thread-ready queue System.out.println ("Put eggs") blocking the queue;} Static class Addthread implements Runnable{private Plate Plate;private object Egg=new object ();p ublic addthread (Plate PLA TE) {this.plate=plate;} public void Run () {Plate.putegg (egg);}} Static class GetThread implements Runnable{private Plate plate;public getthread (Plate Plate) {this.plate=plate;} public void Run () {Plate.getegg ();}} public static void Main (string[] args) {Plate plate=new Plate (); for (int i=0;i<6;i++) {new Thread (new Addthread (Plate)) . StarT (); New Thread (New GetThread (plate)). Start ();}} 

  

The program begins, a thread determines whether the plate is empty, puts an egg, and wakes up a thread in the blocking queue, and the blocking queue is empty, assuming that the CPU has dispatched a thread of a, the plate is not empty, execution waits, the a thread enters the blocking queue, then a B thread executes, the plate is not empty, the egg is taken away And wakes up the a thread that blocks the queue, a thread enters the ready queue, and the ready queue is a thread, executes immediately, and puts eggs; If you repeat the a thread again, the second step in the B thread repeats, the whole process is that the producer (a thread) produces the eggs, and the consumer (b thread) consumes the eggs.

Thread collaboration-Producer/consumer issues

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.