多线程中的生产者消费者案例

生产者消费者

Object类常用的方法

方法功能
void wait()用于使得线程进入等待状态,直到其它线程调用notify()或notifyAll()方法
void wait(long timeout)用于进入等待状态,直到其它线程调用方法或参数指定的毫秒数已经过去为止
void notify()用于唤醒等待的单个线程
void notifyAll()用于唤醒等待的所有线程

案例描述

在一个固定大小的仓库中存放物品,当仓库不为满的时候,生产者生产,同时消费者还在消费,实现同步机制

  • 仓库代码:
package 线程.ProduceAndConsumer;

public class StoreHouse {

    private int cnt = 0; // 描述商品数量

    public synchronized void produceProduct() {
        notify();
        if (cnt < 10) {
            System.out.println("线程" + Thread.currentThread().getName() + "正在生产第" + (cnt + 1) + "个商品");
            cnt ++;
        } else {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public synchronized void produceConsumer() {
        notify();
        if (cnt > 0) {
            System.out.println("线程" + Thread.currentThread().getName() + "正在消费第" + cnt + "个商品");
            cnt --;
        } else {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

  • 生产者代码:
package 线程.ProduceAndConsumer;

public class ProduceThread implements Runnable{


    // 声明一个仓库类型的引用作为成员变量,为了能够调用仓库类中的方法 合成服用原则
    private StoreHouse storeHouse;

    public ProduceThread() {
    }

    public ProduceThread(StoreHouse storeHouse) {
        this.storeHouse = storeHouse;
    }

    @Override
    public void run() {
        while (true) {
            storeHouse.produceProduct();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}

  • 消费者代码:
package 线程.ProduceAndConsumer;

public class ConsumerThread implements Runnable{

    private StoreHouse storeHouse;

    public ConsumerThread() {
    }

    public ConsumerThread(StoreHouse storeHouse) {
        this.storeHouse = storeHouse;
    }

    @Override
    public void run() {
        while (true) {
            storeHouse.produceConsumer();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

  • 实现类代码:
package 线程.ProduceAndConsumer;

public class Test {

    public static void main(String[] args) {
        StoreHouse storeHouse = new StoreHouse();

        ProduceThread produceThread = new ProduceThread(storeHouse);

        ConsumerThread consumerThread = new ConsumerThread(storeHouse);

        Thread t1 = new Thread(produceThread);
        Thread t2 = new Thread(consumerThread);

        t1.start();
        t2.start();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术很low的瓜贼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值