전형 적 인 생산자 소비자 모델

5918 단어 디자인 모드
다음은 전형 적 인 생산자 소비자 모델 표기 법 으로 주석 이 비교적 뚜렷 하여 모두 가 공부 하기에 편리 하 다.

import HomeWork01.Container.Producer;

/**
 *    ,              , put(),take()    ;
 *@project Java_pro
 */
class Container {
    /**
     * size:    ;
     * array:     ;
     * sth:       ,       sth--;
     */
    private int size;
    private Object[] array;
    private volatile Integer sth = 100;

    /*          */
    public Container(int cap) {
        this.array = new Object[cap];
    }

    /**
     * tName:      
     * @param obj:       
     *           ,        ,  wait()  ,          ,   ;
     *    size    obj,    tName  obj,   ;
     *                wait()   ,     ;
     */
    public synchronized void put(Object obj) {
        String tName = Thread.currentThread().getName();

        while (size == array.length) {
            System.out.println("      。");
            try {
                wait();
            } catch (InterruptedException e) {
            }
        }
        array[size] = obj;
        System.out.println("Thread " + tName + " put: " + obj);
        size++;
        notifyAll();
    }

    /**
     * tName:      
     *           ,        ,  wait()  ,          ,   ;
     *         ,  tName  obj;
     *     1           0     -1 ,  size-1   ,size--;
     *                wait()   ,     ;
     */
    public synchronized void take() {
        String tName = Thread.currentThread().getName();
        while (size == 0) {
            System.out.println("      。");
            try {
                wait();
            } catch (InterruptedException e) {
            }
        }
        Object temp = array[0];
        System.out.println("Thread " + tName + " take:" + temp);
        System.arraycopy(array, 1, array, 0, size - 1);
        size--;
        array[size] = null;

        notifyAll();
    }

    /*       put()      sth*/
    class Producer extends Thread {
        Container container;

        public Producer(Container container) {
            this.container = container;
        }

        @Override
        public void run() {
            while (true) {
                container.put(sth);
                --sth;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
            }
        }
    }

    /*       take()      sth*/
    class Consumer extends Thread {
        Container container;
        Object temp;

        public Consumer(Container container) {
            this.container = container;
        }

        @Override
        public void run() {
            while (true) {
                container.take();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
            }

        }
    }
}

public class Producer_ConsumerDemo {
    public static void main(String[] args) throws InterruptedException {
        Container c = new Container(5);

        Producer pro1 = c.new Producer(c);
        pro1.start();
        c.new Consumer(c).start();

    }

}

좋은 웹페이지 즐겨찾기