当前位置: 首页 > news >正文

实验报告2-多线程并发

实验报告2-多线程并发

一、实现思路

        生产者消费者问题描述了共享固定大小缓冲区的两个线程——即所谓的“生产者”和“消费者”——在实际运行时会发生的问题。生产者的主要作用是生成一定量的数据放到缓冲区中,然后重复此过程。与此同时,消费者也在缓冲区消耗这些数据。该问题的关键就是要保证生产者不会在缓冲区满时加入数据,消费者也不会在缓冲区中空时消耗数据。要求:

        1、在缓冲区为空时,消费者不能再进行消费

        2、在缓冲区为满时,生产者不能再进行生产

        3、在一个线程进行生产或消费时,其余线程不能再进行生产或消费等操作,即保持线程间的同步

        4、注意条件变量与互斥锁的顺序

二、实验步骤

Store仓库类

public class Store {
    // 仓库容量
    private int capacity;
    // 底层数据结构
    private List list = new LinkedList<Object>();
    public int getCapacity() {
        return capacity;
    }
    public List getList() {
        return list;
    }
    // 构造函数,为仓库容量赋值
    protected Store(int capacity) {
        this.capacity = capacity;
    }
}

Producer生产者类

public class Producer implements Runnable{
​
    //仓库
    private Store store;
​
    //构造方法,实例化仓库
    public Producer(Store store) {
        this.store = store;
    }
​
    @Override
    public void run() {
        while (true){
            produce();
        }
    }
​
    private synchronized void produce() {
        // 仓库未满,生产商品,唤醒消费者
        if (store.getList().size() <store.getCapacity()) {
            // 生产商品
            store.getList().add(new Object());
            // 唤醒消费者
            notifyAll();
            System.out.println(String.format("%s,生产了一件商品,仓库商品数为%d,唤醒消费者",
                Thread.currentThread().getName(),store.getList().size()));
            // 休眠
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        // 仓库已满,休眠,等待被唤醒
        else {
            try {
                System.out.println(String.format("仓库已满,%s,休眠",Thread.currentThread().getName()));
                wait(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

Consumer消费者类

public class Consumer implements Runnable{

    //仓库
    private Store store;
​
    //构造方法,实例化仓库
    public Producer(Store store) {
        this.store = store;
    }
​
    @Override
    public void run() {
        while (true){
            consume();
        }
    }
​
    private synchronized void consume() {
        // 仓库不为空,消费商品,唤醒生产者
        if (store.getList().size() > 0) {
            // 消费商品
            store.getList().remove(0);
            // 唤醒生产者
            notifyAll();
            System.out.println(String.format("%s,消费了一件商品,仓库商品数为%d,唤醒生产者",
                    Thread.currentThread().getName(),store.getList().size()));
            // 睡眠
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        // 仓库为空,休眠,等待被唤醒
        else {
            try {
                System.out.println(String.format("仓库为空,%s,休眠",Thread.currentThread().getName()));
                wait(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

        Test测试类

public class Test {
    public static void main(String[] args) {
        //实例化新建仓库对象,设置仓库的容量
        Store store = new Store(2);
        //实例化生产者对象
        Producer producer = new Producer(store);
        //实例化消费者对象
        Consumer consumer = new Consumer(store);
​
        //创建生产者线程
        new Thread(producer,"生产者1").start();
        new Thread(producer,"生产者2").start();
        //创建消费者线程
        new Thread(consumer,"消费者1").start();
        new Thread(consumer,"消费者2").start();
    }
}

相关文章:

  • KuberSphere 安装kubernates
  • SVM兵王问题
  • Intel HDSLB 高性能四层负载均衡器 — 基本原理和部署配置
  • 设计模式八股文
  • Vue 跨域代理设置
  • Midjourney Describe API 使用文档
  • 专业渗透测试 Phpsploit-Framework(PSF)框架软件小白入门教程(十三)
  • 03_前端三大件CSS
  • C++小游戏 合集
  • 人人皆是黑客?EvilProxy推出一键反向代理服务
  • MATLAB学习:频谱图的绘制
  • 基于开源ATmega8 无感BLDC程序移植到ATmega328PB
  • 几种常用的配置文件格式对比分析——ini、json、xml、toml、yaml
  • python类和对象
  • 一周股市价格为[2,6,1,4,8],求哪一天买入哪一天卖出,可获得最大收益,最大收益为多少——Java实现,详细注释
  • latex文字竖排
  • 美业美容院会员服务预约店铺管理小程序的效果是什么
  • React Native 之 接口请求(十四)
  • 调用其他程序(不用import call tranction)
  • C语言结构体详解
  • 纪念|海上金石学的兴盛与王昶《金石萃编》
  • 我国核电总体规模首次跃居世界第一,发电量持续增长
  • 中公教育:去年全面扭亏,经营性现金流增长169.6%
  • 魔都眼·上海车展⑥|周六客流超13.5万人次,创开展新高
  • 网络游戏用户规模和市场销售创新高,知识产权保护面临哪些挑战?
  • 最高法改判一起植物新品种侵权案:判赔逾5300万元破纪录