Consumer VS Producer

Source: Internet
Author: User

In the producer and consumer models, you must ensure the following:
1. Only one producer can produce data at a time.
2. Only one consumer can consume at a time.
3. The producer cannot consume the product at the same time.
4 when the message queue is full, the producer cannot continue production.
5 when the message queue is empty, the consumer cannot continue to consume

package ying.threadWait;import java.util.Vector;public class TestConsumer {public static void main(String[] args) { WareHouse warehouse = new WareHouse(0) ; Producer pro1 = new Producer(15 , warehouse) ;  Consumer cust1 = new Consumer(30 , warehouse) ;  Consumer cust2 = new Consumer(20 , warehouse) ;  Consumer cust3 = new Consumer(15 , warehouse) ;  pro1.start() ;  cust1.start() ; cust2.start() ; cust3.start() ; System.out.println("Bye"); }}class WareHouse { public int cur_size ; public final static int max_size = 50 ; public WareHouse() { }public WareHouse(int size) { this.cur_size = size ; }public synchronized void produce(int produceNum) { while (produceNum + cur_size > max_size) { System.out.println("The quantity of product to produce is " + produceNum + " which is over the remaining storage limit : " + (max_size - cur_size));try {System.out.println("Producer " + Thread.currentThread().getName() + " waits"); wait() ;} catch (InterruptedException e) {e.printStackTrace();} }cur_size += produceNum ; System.out.println(Thread.currentThread().getName() + " has produced " + produceNum + " pieces of product."); notifyAll() ; }public synchronized void consume(int needNum) { while (needNum > cur_size) { try {System.out.println("Consumer " + Thread.currentThread().getName() + " waits"); wait() ;} catch (InterruptedException e) {e.printStackTrace();} }cur_size -= needNum ; System.out.println("The customer has consumed " + needNum + " pieces of product.");notifyAll() ; }}class Producer extends Thread { private int produceNum ; private WareHouse warehouse ; Producer(int num , WareHouse house) { this.produceNum = num ;  this.warehouse = house ; } public void run() { while (true) { warehouse.produce(produceNum) ; } }}
class Consumer extends Thread { private int needNum ; private WareHouse warehouse ; Consumer(int num , WareHouse warehouse) { this.needNum = num ; this.warehouse = warehouse ; }public void run() { warehouse.consume(needNum) ; }}

When free can refer to: http://www.blogjava.net/amigoxie/archive/2007/04/11/110006.html

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.