Java学习-生产者消费者问题

面包类-生产的对象

public class Bread {
    private int id;
    private String productName;

    public Bread() {
    }
    public Bread(int id, String productName) {
        super();
        this.id = id;
        this.productName = productName;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    @Override
    public String toString() {
        return "Bread{" +
                "id=" + id +
                ", productName='" + productName + '\'' +
                '}';
    }
}

BreadCon.class-容器

public class BreadCon {
    //存放面包的数组
    private Bread[] cons=new Bread[6];
    //存放面包的位置
    private int index=0;
    //存放面包
    public synchronized void input(Bread b){
        //判断有没有满
        while (index>=6){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        cons[index]=b;
        System.out.println(Thread.currentThread().getName()+"生产了"+b.getId()+"");
        index++;
        //唤醒
        this.notifyAll();
    }
    //取出面包
    public synchronized void output(){
        while (index<=0){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        index--;
        Bread b=cons[index];
        System.out.println(Thread.currentThread().getName()+"消费了"+b.getId()+"生产者"+b.getProductName());
        cons[index]=null;
        //唤醒
        this.notifyAll();
    }
}

Proudct.class-生产者

public class Product implements Runnable {
    private BreadCon con;
    public Product(BreadCon con) {
        this.con = con;
    }
    @Override
    public void run() {
        for (int i = 0; i <30 ; i++) {
            con.input(new Bread(i,Thread.currentThread().getName()));
        }
    }
}

Consume.class-消费者

public class Consume implements Runnable{
    private BreadCon con;
    public Consume(BreadCon con) {
        this.con = con;
    }
    @Override
    public void run() {
        for (int i = 0; i <30; i++) {
            con.output();
        }
    }
}

TestBread.class-测试类
下面展示一些 内联代码片

public class TestBread {
    public static void main(String[] args) {
        //容器
        BreadCon con=new BreadCon();
        //生产和消费
        Product product=new Product(con);
        Consume consume=new Consume(con);
        //创建消费对象
        Thread chenchen=new Thread(product,"chenchen");
        Thread mingming=new Thread(consume,"mingming");
        Thread lili=new Thread(product,"lili");
        Thread bingbing=new Thread(consume,"bingbing");
        chenchen.start();
        lili.start();
        mingming.start();
        bingbing.start();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值