Multi-thread producer and consumer issues

Source: Internet
Author: User

Before I felt very simple, but one interview let me write on the paper, actually did not write to disgrace ah.

Producer consumer problem (Producer-consumer problem): Producers constantly produce products, consumers take away the products produced by producers. The producer produces the product and puts it into an area where consumers remove data from it.

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

Mainly related to: Multithreading synchronization problem.

1, assuming that the producer Line Cheng gang to the data storage space to add the product name, has not added 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 began to take products, or is, consumers to play a product, has not yet waited for producers to produce new products, there are

Repeated removal of the products that have been gone.


Its producer consumer problem program is implemented 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) {///If there is a product, wait for the consumption tr y {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, can be produced by 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.