运行效果:
源代码:
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- /**
- * 操作系统课程实验——缓冲池的模拟使用
- */
- public class Test {
- // 1.创建空缓冲队列
- public List<Buffer_in> emq;
- // 2.创建输入数据缓冲队列
- public List<Buffer_in> inq;
- // 3.创建输出数据缓冲队列
- public List<Buffer_in> outq;
- // 4.创建30个缓冲单元
- public Buffer_in[] buffer;
- // 1.从type类型缓冲队列头取出一个缓冲区
- public Buffer_in takeBuf(List<Buffer_in> type) {
- Buffer_in temp = (Buffer_in) type.remove(0);
- return temp;
- }
- // 2.包含了同步和互斥控制的takeBuf操作
- public Buffer_in getBuf(List<Buffer_in> type) {
- Buffer_in temp = null;
- synchronized (type) {
- temp = takeBuf(type);
- }
- return temp;
- }
- // 3.将buffer插入type类型队列尾
- public void addBuf(List<Buffer_in> type, Buffer_in buffer) {
- type.add(type.size(), buffer);
- }
- // 4.包含了同步和互斥控制的addBuf操作
- public void putBuf(List<Buffer_in> type, Buffer_in buffer) {
- synchronized (type) {
- addBuf(type, buffer);
- }
- }
- public Test() {
- emq = new ArrayList<Buffer_in>();
- inq = new ArrayList<Buffer_in>();
- outq = new ArrayList<Buffer_in>();
- buffer = new Buffer_in[30];
- for (int i = 0; i < 30; i++) {
- buffer[i] = new Buffer_in();
- buffer[i].setBufNo(i);
- buffer[i].setBuf(0);
- emq.add(buffer[i]);
- }
- Input input = new Input(this);
- input.start();
- Output output = new Output(this);
- output.start();
- Compute compute = new Compute(this);
- compute.start();
- }
- class Buffer_in {
- // 1.缓冲区号
- private int BufNo;
- // 2.缓冲区内容
- private int buf;
- public int getBufNo() {
- return BufNo;
- }
- public void setBufNo(int bufNo) {
- BufNo = bufNo;
- }
- public int getBuf() {
- return buf;
- }
- public void setBuf(int buf) {
- this.buf = buf;
- }
- }
- class Input extends Thread {
- public Scanner sc;// 输入内容
- private int a;
- public Test main;
- public Input(Test main) {
- this.main = main;
- }
- public void run() {
- sc = new Scanner(System.in);
- while (true) {
- try {
- sleep(600);
- } catch (Exception e) {
- e.printStackTrace();
- }
- // a=sc.nextInt();//进行数据输入
- a = (int) (Math.random() * 100);// 避开手动输入的麻烦,2者选一
- if (main.emq.size() != 0) { // 首先要判断空队列是否为空
- Buffer_in hin = main.getBuf(main.emq);// 获取空闲缓冲池
- hin.setBuf(a);// 将输入数据(整型数)写入hin
- main.putBuf(main.inq, hin);// 将输入的缓冲池挂到输入队列里面
- System.out.println("输入进程: 缓冲单元" + hin.getBufNo()
- + " 收容输入: data=" + hin.getBuf());
- }
- }
- }
- }
- class Output extends Thread {
- public Test main;
- public Output(Test main) {
- this.main = main;
- }
- public void run() {
- while (true) {
- try {
- sleep(1000);// 休息2秒
- if (main.outq.size() != 0) {
- Buffer_in sout = main.getBuf(main.outq);// 获取输出缓冲池
- System.out.println("输出进程: 缓冲单元" + sout.getBufNo()
- + " 提取输出: data=" + sout.getBuf());
- sout.setBuf(0);
- main.putBuf(main.emq, sout);// 将输入的缓冲池挂到输入队列里面
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- class Compute extends Thread {
- public Test main;
- public Compute(Test main) {
- this.main = main;
- }
- public void run() {
- while (true) { // 5.计算流程提取输入和收容输出
- try {
- sleep(200);
- if ((main.inq.size() != 0) && (main.emq.size() != 0)) {
- Buffer_in sin = main.getBuf(main.inq);
- int temp = sin.getBuf();
- System.out.println("计算进程: 缓冲单元" + sin.getBufNo()
- + " 提取输入: data=" + sin.getBuf());
- main.putBuf(main.emq, sin);
- temp = temp + 10000;// 模拟计算
- Buffer_in hout = main.getBuf(main.emq);
- hout.setBuf(temp);
- main.putBuf(main.outq, hout);
- System.out.println("计算进程: 缓冲单元" + hout.getBufNo()
- + " 收容输出: data=" + hout.getBuf());
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- public static void main(String[] args) {
- new Test();
- }
- }