题目:生产消费问题

题目:

方法1:(有问题)

package com.gao.Project.Pro9;

public class Product {//商品类
    //品牌
    private String brand;
    //名字
    private String name;

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

package com.gao.Project.Pro9;

public class PuoducerThread extends Thread{ //生产者线程
    //共享商品
    private Product p ;
    //构造器
    public PuoducerThread(Product p) {
        this.p = p;
    }

    @Override
    public void run() {
        for (int i = 1; i <= 10 ; i++) {//生产10个商品   i--->生产次数
            synchronized (p){
                if(i%2 == 0){
                    //生成费列罗巧克力
                    p.setBrand("费列罗");
                    p.setName("巧克力");
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }else {
                    //生产哈尔滨啤酒
                    p.setBrand("哈尔滨");
                    p.setName("啤酒");
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
            //将生产的产品打印出来
            System.out.println("生产者生产了"+p.getBrand()+"---"+p.getName());
        }
    }
}

package com.gao.Project.Pro9;

public class CustomerThread extends Thread{//消费者线程
    //共享商品
    private Product p;

    public CustomerThread(Product p) {
        this.p = p;
    }

    @Override
    public void run() {
        for (int i = 1; i <=10 ; i++) {// 消费次数
           synchronized (p){//用同一个锁锁住,解决同步问题-->防止数据错乱--->费列罗啤酒,哈尔滨巧克力
               System.out.println("消费者消费了"+p.getBrand()+"---"+p.getName());
           }
        }
    }
}

package com.gao.Project.Pro9;

public class Test {
    public static void main(String[] args) {
        //共享的商品
        Product p = new Product();
        //创建生产者和消费者线程
        PuoducerThread pt = new PuoducerThread(p);
        CustomerThread ct = new CustomerThread(p);
        pt.start();
        ct.start();
    }
}

方法2:(同步)

package com.gao.Project.Pro10;

public class PuoducerThread extends Thread{ //生产者线程
    //共享商品
    private Product p ;
    //构造器
    public PuoducerThread(Product p) {
        this.p = p;
    }

    @Override
    public void run() {
        for (int i = 1; i <= 10 ; i++) {//生产10个商品   i--->生产次数
            if(i % 2 == 0){
                p.setProduct("费列罗","巧克力");
            }else {
                p.setProduct("哈尔滨","啤酒");

            }

        }
    }
}

package com.gao.Project.Pro10;

public class CustomerThread extends Thread{//消费者线程
    //共享商品
    private Product p;

    public CustomerThread(Product p) {
        this.p = p;
    }

    @Override
    public void run() {
        for (int i = 1; i <=10 ; i++) {// 消费次数
            p.getProduct();
           }
      }
}

package com.gao.Project.Pro10;

public class Product {//商品类
    //品牌
    private String brand;
    //名字
    private String name;

    //引入一个灯    ture:红灯   false:绿灯
    boolean flag = false;  //默认情况没有商品,生产者得生产了之后才能消费

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    //生产商品
    public synchronized void setProduct(String brand,String name){
        if(flag == true){//灯是红的,证明有商品,生产者不生产,等待消费者消费
            try {
                wait();  //出现异常,用try catch 捕获
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        //灯是绿的,证明没有商品,就生产
            this.setBrand(brand);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            this.setName(name);

        //将生产的产品打印出来
        System.out.println("生产者生产了"+this.getBrand()+"---"+this.getName());

        //生产完以后,灯变色
        flag = true;
        //告诉消费者来消费
        notify();
    }

    //消费商品
    public synchronized void getProduct (){
        if(!flag){//(flag == false) 灯是绿的,证明没有商品,消费者需要等待
            try {
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        //灯是红的,证明有商品,可以消费
        System.out.println("消费者消费了"+this.getBrand()+"---"+this.getName());
        //消费完,灯变色
        flag = false;
        //告诉生产者赶紧生产
        notify();
    }
}

package com.gao.Project.Pro10;

public class Test {
    public static void main(String[] args) {
        //共享的商品
        Product p = new Product();
        //创建生产者和消费者线程
        PuoducerThread pt = new PuoducerThread(p);
        CustomerThread ct = new CustomerThread(p);
        pt.start();
        ct.start();
    }
}

posted @ 2022-10-09 09:42  进步+吃饭  阅读(19)  评论(0)    收藏  举报