Producer consumer model -Java code implementation

What is a producer - consumer model

  For example, two processes A and B, which shared buffer, process A generates a fixed-size data into the buffer, B calculation process data extracted from the buffer, this is actually a producer and consumer model , a is equivalent to the producer, B is equivalent to the consumer, producer-consumer problem to be solved is how to deal with public resources.

Producer - consumer model features

  • To ensure that the producers will not continue to put data into the buffer in the buffer is full, and consumers will not be in the buffer zone empty, consumption data
  • When the buffer is full, the producer will go to sleep, when consumers start to consume the next data buffer, the producer will wake up, start adding data to the buffer; when the buffer is empty, consumers It will go to sleep, to be awakened only when data is added to the buffer until producer

Code

package com.rao.operatingSystem;

/**
 * @author Srao
 * @className ProducerAndConsumer
 * @date 2019/12/20 22:51
 * @package com.rao.operatingSystem
 * @Description 生产者消费者模型
 */
public class ProducerAndConsumer {
    public static void main(String[] args) {
        Factory factory = new Factory(10);

        Producer producer = new Producer(factory);
        Producer producer2 = new Producer(factory);
        Consumer consumer = newConsumer (Factory); 

        producer.start (); 
        producer2.start (); 
        consumer.start (); 
    } 


    / * * 
     * factory model, representing common resources 
     * / 
    static  class Factory's {
         int max; // Maximum number of items of plant 
        int NUM; // this plant there are many items which 

        public factory's ( int max) {
             the this .max = max; 
        } 

        / * * 
         * production 
         * / 
        the synchronized void the Add () {
             // If the plant is not full, on the production of an article 
            if (num <max) { 
                NUM ++ ; 
                . System OUT .println ( " production of a commodity, there are now factory: " + NUM + " items " );
                 // wake of consumers waiting to spend 
                notifyAll (); 
            } the else {
                 the try {
                     // factory full producer wait 
                    the wait (); 
                    . the System OUT .println ( " factory full, waiting producer " ); 
                } the catch (InterruptedException E) {
                    e.printStackTrace (); 
                } 
            } 
        } 

        the synchronized void Remove () {
             // If there are items plant 
            IF ( 0 < NUM) { 
                NUM - ; 
                . the System OUT .println ( " consumed one item, left: " NUM + + " items " );
                 // wake producer to wait to produce articles 
                notifyAll (); 
            } the else {
                 the try {
                     // nothing can be consumed, the thread waits until the producers to produce
                    the wait (); 
                    . the System OUT .println ( " factory nothing to the consumer waiting " ); 
                } the catch (InterruptedException E) { 
                    e.printStackTrace (); 
                } 
            } 
        } 
    } 

    / * * 
     * Manufacturer 
     * / 
    static  class Producer the Thread {the extends 
        Factory's Factory; 

        public Producer (Factory's Factory) {
             the this .factory = Factory; 
        } 

        @Override 
        public  void run() {
            while (true){
                //一直生产
                try{
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                factory.add();
            }
        }
    }

    /**
     * 消费者
     */
    static class Consumer extends Thread{
        Factory factory;

        public Consumer(Factory factory) {
            this.factory = factory;
        }

        @Override
        public void run() {
            while (true){
                //一直消费
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                factory.remove();
            }
        }
    }
}

Guess you like

Origin www.cnblogs.com/rao11/p/12075523.html