Producer and consumer problems-java Implementation

Source: Internet
Author: User


Consumer issues, also known as limited buffer issues. Two problems to be solved:

1. Synchronize (save and retrieve) Resources in the buffer zone)

2. When the buffer zone is full, it cannot be stored and the producer needs to wait. When the buffer zone is empty, it cannot be obtained and the consumer needs to wait.


Solution:

For Question 1: Add synchronized to the access method

Problem 2: implemented through thread waiting and Wakeup


Check the Code directly.

There are four main categories: commodities, buffers, producers, and consumers.

1. Commodities

class Product {private int productId = 0;public Product(int productId) {this.productId = productId;}public int getProductId() {return productId;}public void setProductId(int productId) {this.productId = productId;}}

2. Buffer Zone

Class Buffer {private int base = 0; private int top = 0; // Buffer size private Product [] products = new Product [10]; /*** production product *** @ param Product */public synchronized void push (product) {if (top = products. length) {// If the buffer is full, wait for consumption try {System. out. println ("the buffer is full and is waiting for consumption .. "); super. wait ();} catch (InterruptedException e) {System. out. println ("stop push product because other reasons") ;}// if the buffer is not full, store the produced product into the database: products [top] = product; // number of products in the database + 1top ++; super. Y ();} public synchronized Product pop () {Product product = null; while (top = base) {// the buffer is empty and cannot consume consumer y (); try {System. out. println ("the buffer is empty, waiting for production... "); super. wait ();} catch (InterruptedException e) {System. out. println ("stop push product because other reasons") ;}// the buffer is not empty. You can consume top --; product = products [top]; products [top] = null; super. Y (); return product ;}}

3. Producer

Class Producer implements Runnable {private String producerName; private Buffer; public Producer (String producerName, Buffer) {this. producerName = producerName; this. buffer = Buffer;} public void setProducerName (String producerName) {this. producerName = producerName;} public String getProducerName () {return producerName;} @ Overridepublic void run () {execProcuct ();} private void execProcuct () {int I = 0; while (true) {I ++; Product pro = new Product (I); Buffer. push (pro); System. out. println (getProducerName () + "produced:" + pro. getProductId (); try {Thread. sleep (1000);} catch (InterruptedException e) {return ;}}}}

4. Consumers

Class Consumer implements Runnable {private String consumerName = null; private Buffer = null; public Consumer (String consumerName, Buffer) {this. consumerName = consumerName; this. buffer = Buffer;} public void setConsumerName (String consumerName) {this. consumerName = consumerName;} public String getConsumerName () {return consumerName;} public void execConsume () {while (true) {System. out. println (getConsumerName () + "consumed:" + Buffer. pop (). getProductId (); try {Thread. sleep (500);} catch (InterruptedException e) {return ;}}@ Overridepublic void run () {execConsume ();}}

5. Entrance

Public class ProducerConsumer {public static void main (String [] args) {Buffer = new Buffer (); Producer producer = new Producer ("++ Producer", Buffer ); consumer comsumer = new Consumer ("-Consumer", Buffer); Thread t1 = new Thread (producer); Thread t2 = new Thread (comsumer); t1.start (); t2.start ();}}


Refer:

Http://www.cnblogs.com/rollenholt/archive/2011/08/28/2156357.html

Http://www.cnblogs.com/dennisit/archive/2013/02/25/2931573.html

Http://blog.chinaunix.net/uid-21411227-id-1826740.html

We hereby declare that.



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.