多线程通信(with;notify)

多线程通信(with;notify)
  1. 在Java对象中,有两种池

    琐池-----------------synchronized;

    等待池-—--wait() ,notify() , notifyAll()

    如果一个线程调用了某个对象的wait方法,那么该线程进入到wait等待池中(并且已经将锁释放);如果未来的某一时刻,另外一个线程调用了相同对象的notify方法或者notifyAll方法,那么该等待池中的线程;去执行wait后的方法

        package com.Tread01;
        public class Product {
            private String proName;
            private String proType;
            //true为等待红灯;false为绿灯;默认没有商品,红灯先进行生产;生产之后变成绿灯进行消费
            boolean flag = true;
            public Product() {
            }
            public String getProName() {
                return proName;
            }
            public void setProName(String proName) {
                this.proName = proName;
            }

            public String getProType() {
                return proType;
            }

            public void setProType(String proType) {
                this.proType = proType;
            }

            public synchronized void setProduct(String Name,String Type){
                if(!flag){//!true是指绿灯
                    try {
                        //一开始默认是红灯生产;当为!flag绿灯时;进行等待消费
                        wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                      this.setProName(Name);
                       this.setProType(Type);
                System.out.println("生产者生产了" + this.getProName() + "----" + this.getProType());
                //生产完后变灯;开始消费
                flag = false;
                //激活消费线程中with后的代码块
                notify();
            }
            //消费者和生产者公用一个产品属性
            public synchronized void getConsumer(){
                if(flag){//flag是指红灯
                    try {
                        //当另一个线程执行完;释放线程后,会回到wait处后继续执行
                        wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("消费者消费了" + this.getProName() + "----" + this.getProType());
                flag = true;
                notify();
            }
        }

        package com.Tread01;
        public class Consumer extends Thread{
            private Product p;
            public Consumer(Product p){
                this.p = p;
            }
            public void run() {
                    for (int i = 1; i <=10; i++) {
                        p.getConsumer();
                    }
            }
        }

        package com.Tread01;
        public class Producer extends Thread {
            private Product p;
            public Producer(Product p) {
                this.p = p;
            }
            @Override
            public void run() {
                for (int nub = 1; nub <= 10; nub++) {
                    if (nub % 2 == 0) {
                        p.setProduct("哈尔滨", "啤酒");
                    } else {
                        p.setProduct("菲律宾", "巧克力");
                    }
                }
            }
        }

        package com.Tread01;
        public class TestDemo001 {
            public static void main(String[] args)  {
                 Product p =new Product();
                Producer producer =new Producer(p);
                producer.start();
                Consumer consumer = new Consumer(p);
                consumer.start();
            }
        }

posted @ 2022-04-20 10:34  爱豆技术部  阅读(109)  评论(0)    收藏  举报