经典多线程问题-生产者消费者问题

/**
 * @ClassName Question09
 * @Description: 经典生产者消费者问题
 * @Author xtanb
 * @Date 2019/10/21
 * @Version V1.0
 **/
public class Question09 {
    class CubbyHole{
        private int seq;
        private boolean able = false;

        public synchronized int get(){
            while(!able){
                try {
                    wait();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            able=false;
            notify();
            return seq;
        }

        public synchronized void put(int value){
            while(able){
                try {
                    wait();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            seq =value;
            able = true;
            notify();
        }
    }

    class Producer extends Thread{
        private CubbyHole cubbyHole;
        private int number;

        public Producer(CubbyHole c,int number){
            cubbyHole= c;
            this.number=number;
        }
        public void run(){
            for(int i=0;i<10;i++){
                cubbyHole.put(i);
                System.out.println("Productor"+number +" put "+i);
                try {
                    sleep((int)Math.random()*100);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    class Consumer extends Thread{
        private CubbyHole cubbyHole;
        private int number;

        public Consumer(CubbyHole cubbyHole, int number) {
            super();
            this.cubbyHole = cubbyHole;
            this.number = number;
        }

        public void run(){
            int value =0;
            for(int i=0;i<10;i++){
                value= cubbyHole.get();
                System.out.println("Customer"+number+" get "+value );
            }
        }
    }
    public static void main(String[] args) {
        CubbyHole cubbyHole = new Question09().new CubbyHole();
        Producer p1 =new Question09().new Producer(cubbyHole, 1);
        Consumer c1 =new Question09().new Consumer(cubbyHole, 1);
        p1.start();
        c1.start();
    }
}

 

上一篇:Sicily 1931. 卡片游戏


下一篇:2021-06-02