操作系统生产者消费者问题(java用PV实现)

在这里插入图片描述

理论支持,点我点点我

代码有四部分:

Storage类:

/*
 * @Author: robot-cmy 
 * @Date: 2021-11-21 18:47:47 
 * @Last Modified by: robot-cmy
 * @Last Modified time: 2021-11-22 10:34:46
 */
//https://www.cnblogs.com/wkfvawl/p/11529681.html
package myOperationSystem.ProducerAndConsumer;

public class Storage {
    private int[] mutex = { 1 }; // 互斥信号量mutex
    private int[] empty = { 10 }; // 同步信f非号量,表示空闲缓冲区的数量
    private int[] full = { 0 }; // 同步信号量,表示空闲缓冲区的数量
    public int[] count = { 0 }; // 计数器,结束线程
    private int huowu = 0; // 可用缓冲区的数量

    // p操作
    public synchronized void P(int[] num) {
        num[0]--;
        if (num[0] >= 0) {
            return;
        } else {

            try {
                System.out.println("\t" + Thread.currentThread().getName() + " 被阻塞");
                wait();
                System.out.println("\t" + Thread.currentThread().getName() + " 被唤醒");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    // v操作
    public synchronized void V(int[] num) {
        num[0]++;

        if (num[0] > 0) {
            return;
        } else {
            notify();
        }
    }

    // 生产者方法
    public synchronized void produce() {
        P(empty);
        P(mutex);

        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        huowu++; // 生产完后货物++
        System.out.println(Thread.currentThread().getName() + " 把新产品放入缓冲区,库存还有: " + huowu);
        V(mutex);
        V(full);

    }

    // 消费者方法
    public synchronized void consume() {
        P(full);

        P(mutex);

        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        huowu--; // 消费完后货物--
        System.out.println(Thread.currentThread().getName() + " 从缓冲区取走了物品,库存还有: " + huowu);
        V(mutex);
        V(empty);
    }

}

Consumer类:

/*
 * @Author: robot-cmy 
 * @Date: 2021-11-21 19:13:50 
 * @Last Modified by: robot-cmy
 * @Last Modified time: 2021-11-22 10:16:05
 */
package myOperationSystem.ProducerAndConsumer;

public class Consumer implements Runnable {
    private Storage storage;

    public Consumer() {
    }

    public Consumer(Storage storage) {
        this.storage = storage;
    }

    @Override
    public void run() {
        while (true) {
            storage.count[0]++;

            if (storage.count[0] > 40) {
                break;
            }

            storage.consume();

            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

Producer类:

/*
 * @Author: robot-cmy 
 * @Date: 2021-11-21 19:13:50 
 * @Last Modified by: robot-cmy
 * @Last Modified time: 2021-11-22 10:16:05
 */
package myOperationSystem.ProducerAndConsumer;

public class Consumer implements Runnable {
    private Storage storage;

    public Consumer() {
    }

    public Consumer(Storage storage) {
        this.storage = storage;
    }

    @Override
    public void run() {
        while (true) {
            storage.count[0]++;

            if (storage.count[0] > 40) {
                break;
            }

            storage.consume();

            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

Main类(主类):

/*
 * @Author: robot-cmy 
 * @Date: 2021-11-22 11:16:17 
 * @Last Modified by: robot-cmy
 * @Last Modified time: 2021-11-22 11:51:50
 */
//https://www.cnblogs.com/wkfvawl/p/11538431.html
package myOperationSystem.ReaderAndWriter;

public class Main {
    public static void main(String[] args) {
        // 三个读者一个写者
        Book book = new Book();
        Reader reader1 = new Reader(book);
        Reader reader2 = new Reader(book);
        Reader reader3 = new Reader(book);
        Writer writer = new Writer(book);

        Thread r1 = new Thread(reader1, "读者1:");
        Thread r2 = new Thread(reader2, "读者2:");
        Thread r3 = new Thread(reader3, "读者3:");
        Thread w = new Thread(writer, "写者:");

        r1.start();
        r2.start();
        r3.start();
        w.start();
    }
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值