线程之间的通信之消费者与生产者

线程之间通信

       生产者与消费者:生产者生产水果,消费者购买水果,当消费着购买水果时,要判断有没有水果,如果有,则购买,并且将水果的状态变为无,然后通知生产者没有水果了,请生产,若果无则等待生产者生产水果,生产者生产水果时,也要判断水果的状态,若果还有则不生产,等待消费者购买后再生产,如果没有水果,则生产,生产完后将水果状态变为有,并且通知消费者来购买。

我们首先建一个水果类:

package ThreadConnection;

/**
 * 这是一个水果类,有名称和是否存在两个属性
 */
public class Fruit {
    private String name;
    private boolean isExist;

    public String getName() {
        return name;
    }

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

    public boolean isExist() {
        return isExist;
    }

    public void setExist(boolean exist) {
        isExist = exist;
    }
}

然后建一个生产者类,实现Runnable接口:

package ThreadConnection;

/**
 * 这是一个生产者类
 */
public class FruitProduct implements Runnable{
    //声明一个水果类
    private Fruit fruit;

    public FruitProduct(Fruit fruit) {
        super();
        this.fruit = fruit;
    }

    @Override
    public void run() {
        while (true){
            //同步锁
            synchronized (fruit){
                //如果水果已经存在,就不必再生产,等待消费者来购买即可
                if (fruit.isExist()){
                    try {
                        System.out.println("还没卖完,等待消费者购买"+fruit.getName()+".......");
                        //线程等待中。。。。
                        fruit.wait();

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }


                }
                //如果水果是不存在的,则生产一个水果,将水果的存在属性变为true,并且唤醒消费者的购买线程
                fruit.setExist(true);
                System.out.println(fruit.getName()+"被生产出来了!");
                //唤醒消费者线程
                fruit.notify();
            }
        }
    }
}

再建一个消费者类,实现Runnable接口:

package ThreadConnection;

/**
 * 这是一个消费者类
 */
public class FruitBay implements Runnable{
    private Fruit fruit;

    public FruitBay(Fruit fruit) {
        super();
        this.fruit = fruit;
    }

    @Override
    public void run() {
        while (true){
            //同步锁
            synchronized (fruit){
                //若果水果不存在,则不进行购买,等待生产者生产出水果
                if (!fruit.isExist()){
                    try {
                        System.out.println("买完了,等待生产者生产"+fruit.getName()+".......");
                        //线程等待
                        fruit.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //如果水果存在,则购买水果,并将水果的存在属性变为false,并通知生产者生产水果
                fruit.setExist(false);
                System.out.println(fruit.getName()+"被买走了!");
                //唤醒生产者线程
                fruit.notify();
            }
        }
    }
}

最后建一个测试类:

package ThreadConnection;

/**
 * 测试类
 */
public class TestMain {
    public static void main(String[] args) {
        //实例化一个水果类,并命名为哈密瓜,状态为存在
        Fruit fruit=new Fruit();
        fruit.setName("哈密瓜");
        fruit.setExist(true);
        //实例化一个生产者线程类和一个消费者线程类
        FruitBay fruitBay=new FruitBay(fruit);
        FruitProduct fruitProduct=new FruitProduct(fruit);
        //差UN关键生产者和消费者线程
        Thread t1=new Thread(fruitBay);
        Thread t2=new Thread(fruitProduct);
        //启动线程
        t1.start();
        t2.start();
    }
}
运行结果:




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值