Multi-thread producer and consumer issues

Source: Internet
Author: User

Before feeling very easy, but one interview let me write on the paper, unexpectedly did not write to disgrace ah.

Producer Consumer issues (Producer-consumer problem): Producers continue to produce products. Consumers take away the products produced by the producers. The producer produces the product and puts it into an area. Consumers remove data from this place.

The problem involved: to ensure that producers do not increase data when the buffer is full, consumers will not consume data when the buffer is empty.

Mainly related to: Multithreading synchronization problem.

1, if the producer Line Cheng gang to the data storage space to add the product name, has not been added to the content of the product, the program is cut to the consumer's thread, consumption of this

Threads link the name of the product to the contents of the previous product.

2, the producers put a number of times the product, consumers start to take products, or is, consumers to play a product, have not waited for producers to produce new products, there are

Repeatedly remove the products that have been gone.


The implementation of its producer consumer problem program is as follows:

First, Products:


Package andy.thread.test;/** * @author Andy * @version: 2015-3-20 a.m. 10:09:42 * * */public class Product {private String P Name;private String pcontent;private boolean flag; This is a product marked true for an existing product false for no product//production public synchronized void put (string pName, String pcontent) {if (flag) {///Assuming there is a product. Wait for consumption try {super.wait ();} catch (Interruptedexception e) {e.printstacktrace ();}} This.setpname (pName); try {thread.sleep);} catch (Interruptedexception e) {e.printstacktrace ();} This.setpcontent (pcontent); SYSTEM.OUT.PRINTLN ("Production product"); This.flag = true; Marked for production, wake-up consumption super.notify ();} Consumption public synchronized void Romve () {if (!flag) {///No product waits for try {super.wait ();} catch (Interruptedexception e) {e.printst Acktrace ();}} try {thread.sleep;} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println ("Consumption:" + this.getpname () + "---" + this.getpcontent ()); this.flag = false; has been consumed, able to produce the super.notify ();} Public String Getpname () {return pName;} public void Setpname (String pName) {this.pname = PName;} PUblic String getpcontent () {return pcontent;} public void Setpcontent (String pcontent) {this.pcontent = pcontent;}}

Second, producers

Package Andy.thread.test;import java.util.concurrent.timeunit;/** * @author Andy * @version: 2015-3-20 a.m. 11:05:53 *  *  */public class Producer implements Runnable {Private product Product = Null;public Producer (product product) {THIS.P roduct = Product;} @Overridepublic void Run () {for (int i = 0; i <; i++) {try {TimeUnit.SECONDS.sleep (5);} catch (Interruptedexception e) {e.printstacktrace ();} Product.put ("Product" + I, I + "");}}}

Third, the consumer

Package Andy.thread.test;import java.util.concurrent.timeunit;/**   * @author Andy   * @version: 2015-3-20 a.m. 10 : 56:18   *  *   */public class Consumer implements Runnable{private product Product = Null;public Consumer ( Product product) {this.product = product;} @Overridepublic void Run () {for (int i = 0; i <; i++) {try {TimeUnit.SECONDS.sleep (5);} catch (Interruptedexception e) {e.printstacktrace ();} This.product.romve ();}}}


Test:

Package andy.thread.test;/**   * @author Andy   * @version: 2015-3-20 a.m. 11:12:25   *  *   */public class consumerproducertest {/** * @param args */public static void main (string[] args) {Product Product = new Product (); Producer Producer = new Producer (product); Consumer Consumer = new Consumer (product); new Thread (producer). Start (); new Thread (Consumer). Start ();}

The results are as follows:


Multi-thread producer and 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.