栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

操作系统读者写者问题(java用PV实现)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

操作系统读者写者问题(java用PV实现)

代码共四部分

理论支持,点我点点我 Book类:
//https://www.cnblogs.com/wkfvawl/p/11538431.html
package myOperationSystem.ReaderAndWriter;

public class Book {
    private int[] rw = { 1 }; // 实现对文件的互斥访问,表示当前是否有进程在访问共享文件
    private int count = 0; // 记录有几个读进程在访问文件
    private int[] mutex = { 1 }; // 保证对count变量的互斥访问

    // p操作
    public synchronized void P(int[] num) {
        num[0]--;

        if (num[0] >= 0) {
            return;
        } else {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

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

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

    // 写者write方法
    public void write() {
        P(rw);

        System.out.println(Thread.currentThread().getName() + " 正在写文件");

        V(rw);
        System.out.println(Thread.currentThread().getName() + " 写完文件");
    }

    // 读者read方法
    public void read() {
        P(mutex);

        if (count == 0) {
            P(rw);
        }

        count++;
        V(mutex);
        System.out.println(Thread.currentThread().getName() + " 读文件");
        P(mutex);

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

        count--;

        if (count == 0) {
            V(rw);
        }

        V(mutex);
    }
}

Reader类:
package myOperationSystem.ReaderAndWriter;

public class Reader implements Runnable {
    private Book book;

    public Reader() {
    }

    public Reader(Book book) {
        this.book = book;
    }

    @Override
    public void run() {
        for (int i = 0; i < 30; i++) {
            book.read();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

Writer类:
package myOperationSystem.ReaderAndWriter;

public class Writer implements Runnable {
    private Book book;

    public Writer() {
    }

    public Writer(Book book) {
        this.book = book;
    }

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            book.write();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

Main类(主类):
//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();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/583089.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号