多线程之生产者消费者问题

来源于b站视频:https://www.bilibili.com/video/BV1V4411p7EF?p=25,这里安利下原片【狂神说Java】多线程详解

一、问题概述

是处理并发问题的一个典型应用。生产者进程和消费者进程同时发生,以此修改缓存区Buffer的资源。

生产者消费者模型示意图(来源网络)

 

 

二、模型思路

被管理的资源Good,在缓存区内:

class Goods{
    int id;
    public Goods(int id){
        this.id = id;
    }
}

主程序,再去补充细节:

public class TestPC {
    public static void main(String[] args) {
        Buffer buf = new Buffer();       //缓存区对象
        Producer p = new Producer(buf);   // 生产者进程
        Consumer c = new Consumer(buf);   // 消费者进程
        p.start();
        c.start();
    }
}

分析下需要,实现思路是缓存区类中放有货物的数组,生产者进程调动时货物数目加1,消费者进程调动时货物数目减1.调动的函数在缓存区类中实现

class Buffer{
    // 缓冲区
    Goods[] a = new Goods[10];
    int count ; // 记录缓冲区的元素个数

    //  开始写生产者的方法
    public synchronized void push(Goods good)  {
        // 如果容器满了需要等待消费者消费
        if(count==a.length){
            //当count==容器的容量 生产者等待
            try {
                this.wait();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        // 开始添加
        a[count] = good;
        count++;
        // 通知消费者消费
        this.notifyAll();

    }

    // 开始写消费者的方法
    public synchronized Goods pop() {
        if(count==0){
            // 等待
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        count--;
        Goods good_out = a[count];
        this.notifyAll();
        return good_out;
    }
}

生产者、消费者进程:

class Producer extends Thread{
    private Buffer buf;
    public Producer(Buffer buf){
        this.buf =  buf;
    }
    @Override
    public void run() {
        for(int i = 0;i<100;i++){
            System.out.println("生产了"+i+"件货物");
            buf.push(new Goods(i));
        }
    }
}
class Consumer extends Thread{
    private Buffer buf;
    public Consumer(Buffer buf){
        this.buf = buf;
    }
    @Override
    public void run() {
        for(int i = 0;i<100;i++){
            System.out.println("消费了"+buf.pop().id+"件货物");
        }
    }
}

 自身线程等待用this.wait(),唤醒其他线程用this.notifyAll();

完整代码:

package Thread_higher;

public class TestPC {
    public static void main(String[] args) {
        Buffer buf = new Buffer();       //缓存区对象
        Producer p = new Producer(buf);   // 生产者进程
        Consumer c = new Consumer(buf);   // 消费者进程
        p.start();
        c.start();
    }
}

class Producer extends Thread{
    private Buffer buf;
    public Producer(Buffer buf){
        this.buf =  buf;
    }
    @Override
    public void run() {
        for(int i = 0;i<100;i++){
            System.out.println("生产了"+i+"件货物");
            buf.push(new Goods(i));
        }
    }
}

class Consumer extends Thread{
    private Buffer buf;
    public Consumer(Buffer buf){
        this.buf = buf;
    }
    @Override
    public void run() {
        for(int i = 0;i<100;i++){
            System.out.println("消费了"+buf.pop().id+"件货物");
        }
    }
}

class Buffer{
    // 缓冲区
    Goods[] a = new Goods[10];
    int count ; // 记录缓冲区的元素个数

    //  开始写生产者的方法
    public synchronized void push(Goods good)  {
        // 如果容器满了需要等待消费者消费
        if(count==a.length){
            //当count==容器的容量 生产者等待
            try {
                this.wait();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        // 开始添加
        a[count] = good;
        count++;
        // 通知消费者消费
        this.notifyAll();

    }

    // 开始写消费者的方法
    public synchronized Goods pop() {
        if(count==0){
            // 等待
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        count--;
        Goods good_out = a[count];
        this.notifyAll();
        return good_out;
    }
}

class Goods{
    int id;
    public Goods(int id){
        this.id = id;
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值