生产者消费者

  • if判断的话,唤醒后线程会从wait之后的代码开始运行,但是不会重新判断if条件,直接继续运行if代码块之后的代码
  • while的话,也会从wait之后的代码运行,但是唤醒后会重新判断循环条件, 如果不成立再执行while代码块之后的代码块,成立的话继续wait。
  • 这也就是为什么用while而不用if的原因了,因为线程被唤醒后,执行开始的地方是wait之后。

管程法【缓冲区法】

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 {
            while(counter == container.length){
                this.wait();
            }
            counter++;
            container[counter] = shashlik;
            this.notifyAll();
        }
        /*吃羊肉串*/
        public synchronized  Shashlik c() throws InterruptedException {
            while(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 class FlagPCTest_Shashlik {
    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 {
            while (!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) {
        while (flag) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("吃羊肉串" + num);
        this.notifyAll();
        this.flag = !this.flag;
    }
}
posted @ 2022-07-11 15:25  haveanicedayfh  阅读(40)  评论(0)    收藏  举报