Classic multithreading problem - producers and consumers

/**
 * @ClassName Question09
 * @Description: Classic producers and consumers
 * @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();
    }
}

 

Guess you like

Origin www.cnblogs.com/helloworldmybokeyuan/p/11714948.html