其他分享
首页 > 其他分享> > 生产者消费者

生产者消费者

作者:互联网

管程法【缓冲区法】

public class BufferPCTest_Shashlik {
    public static void main(String[] args) {
      SynContainer container = new SynContainer();
      new Thread(new C(container)).start();
      new Thread(new P(container)).start();

    }
    /*消费者*/
    static class C implements  Runnable{
        private SynContainer container;
        public C(SynContainer container){
            this.container = container;
        }
        @Override
        public void run() {
            for (int i = 0; i < 200; i++) {
                try {
                    System.out.println("吃羊肉串"+container.c().id);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    /*生产者*/
    static class  P implements Runnable{
        private SynContainer container;
        public P(SynContainer container){
            this.container = container;
        }
        @Override
        public void run() {
            for (int i = 0; i < 200; i++) {
                try {
                    container.p(new Shashlik(i));
                    System.out.println("烤羊肉串"+i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    /*盛放羊肉串的容器=>业务逻辑*/
    static class SynContainer {
        int counter = 0;
        Shashlik[] container = new Shashlik[200];
        /*烤羊肉串*/
        public synchronized void p(Shashlik shashlik) throws InterruptedException {
            if(counter == container.length){
                this.wait();
            }
            counter++;
            container[counter] = shashlik;
            this.notifyAll();
        }
        /*吃羊肉串*/
        public synchronized  Shashlik c() throws InterruptedException {
            if(counter == 0){
                this.wait();
            }
            Shashlik shashlik = container[counter];
            counter--;
            this.notifyAll();;
            return shashlik;
        }
    }
    /*羊肉串*/
    static class Shashlik{
        int id;
        public Shashlik(int id){
            this.id = id;
        }
    }
}

标志位法

    public static void main(String[] args) throws InterruptedException {
        Shashlik shashlik = new Shashlik();
        new Thread(new P(shashlik)).start();
        new Thread(new C(shashlik)).start();
    }
}
/*生产者*/
class P implements Runnable {
    Shashlik shashlik;
    public P(Shashlik shashlik) {
        this.shashlik = shashlik;
    }
    @Override
    public void run() {
        for (int i = 1; i <= 200; i++) {
            this.shashlik.p(i);
        }
    }
}
/*消费者*/
class C implements Runnable {
    Shashlik shashlik;
    public C(Shashlik shashlik) {
        this.shashlik = shashlik;
    }
    @Override
    public void run() {
        for (int i = 1; i <= 200; i++) {
            this.shashlik.c(i);
        }
    }
}
/*羊肉串 业务逻辑*/
class Shashlik {
    boolean flag = true;
    public synchronized void p(int num) {
        try {
            if (!flag) {
                this.wait();
            }
            /*烤羊肉串要等哦*/
            TimeUnit.MILLISECONDS.sleep(1000);
            System.out.println("烤羊肉串" + num);
            this.notifyAll();
            this.flag = !this.flag;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    /*吃羊肉串*/
    public synchronized void c(int num) {
        if (flag) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("吃羊肉串" + num);
        this.notifyAll();
        this.flag = !this.flag;
    }
}

标签:container,消费者,生产者,shashlik,new,SynContainer,public,Shashlik
来源: https://www.cnblogs.com/haveanicedayfh/p/16466552.html