资源简介

基于JAVA线程调度的读者写者问题,包括读者优先,写者优先,公平竞争三种代码

资源截图

代码片段和文件信息

package raw;

class DataFile {

    //表示正在进行读取操作的人数
    private int readerCount;
    //doreading 表示读信号量 当 doreading = true 时 表示有线程在读无法进行写操作
    private boolean doreading;
    //dowriting 表示写信号量 当 dowriting = true 时 表示有线程在写无法进行读操作      如何解决一个线程在写让另外其他的不能写?
    private boolean dowriting;
    private int www;

    public DataFile() {
        readerCount = 0;
        doreading = false;
        dowriting = false;
        www = 0;
    }

    public static void naps() {
        try {
            Thread.sleep((int) (4000 * Math.random()));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized int startRead() //开始读操作
    {
        while (dowriting == true) // 当有写线程在完成写操作时  读线程等待写线程唤醒
        {
            try {
                System.out.println(Thread.currentThread().getName() + “临界资源被占用“);
                //等待写者发出notify
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        while(true)    
        {
             if (www == 0) {
                System.out.println(Thread.currentThread().getName() + “开始进行读操作“);
                readerCount++;
                break;
            } else {
                try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            }
        }

        if (readerCount >= 1) {
            doreading = true;
        }
        return readerCount;
    }

    public synchronized int endRead() //结束读操作
    {
        --readerCount;

        if (readerCount == 0) {
            doreading = false;
        }
        notifyAll();
        System.out.println(Thread.currentThread().getName() + “读操作结束“);
        if(readerCount==0)
        {
         System.out.println(“此时无读者在读,临界资源释放“);
        }

        return readerCount;
    }

    public synchronized void startWrite() //开始写操作
    {
        www++;
        while (doreading == true || dowriting == true) {
            try {
                System.out.println(Thread.currentThread().getName() + “临界资源被占用“);
                //等待读者或者写者发出信息
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println(Thread.currentThread().getName() + “开始进行写操作“);
        dowriting = true;
    }

    public synchronized void endWrite() //结束写操作
    {
        --www;
        dowriting = false;
        notifyAll();
        System.out.println(Thread.currentThread().getName() + “写操作结束,释放临界资源“);
    }
}

class Reader implements Runnable {

    private int readerNum;

    private DataFile df;

    Reader(int readerNum DataFile df) {
        this.readerNum = readerNum;
        this.df = df;
    }

    public void run() {
        df.naps();
        df.startRead();
        df.naps();
        

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-05-23 12:07  RAW\
     文件        3594  2018-05-23 11:56  RAW\build.xml
     目录           0  2018-05-23 12:07  RAW\build\
     目录           0  2018-05-23 12:07  RAW\build\classes\
     文件           0  2018-05-23 12:07  RAW\build\classes\.netbeans_automatic_build
     文件           0  2018-05-23 12:07  RAW\build\classes\.netbeans_update_resources
     目录           0  2018-05-23 12:07  RAW\build\classes\raw\
     文件        2237  2018-05-23 12:07  RAW\build\classes\raw\DataFile.class
     文件        1112  2018-05-23 12:07  RAW\build\classes\raw\RAW.class
     文件          47  2018-05-23 12:07  RAW\build\classes\raw\RAW.rs
     文件         605  2018-05-23 12:07  RAW\build\classes\raw\Reader.class
     文件         599  2018-05-23 12:07  RAW\build\classes\raw\Writer.class
     文件          85  2018-05-23 11:56  RAW\manifest.mf
     目录           0  2018-05-23 11:56  RAW\nbproject\
     文件       79614  2018-05-23 11:56  RAW\nbproject\build-impl.xml
     文件         475  2018-05-23 11:56  RAW\nbproject\genfiles.properties
     目录           0  2018-05-23 12:10  RAW\nbproject\private\
     文件         112  2018-05-23 11:56  RAW\nbproject\private\private.properties
     文件         427  2018-05-23 12:10  RAW\nbproject\private\private.xml
     文件        2461  2018-05-23 11:56  RAW\nbproject\project.properties
     文件         511  2018-05-23 11:56  RAW\nbproject\project.xml
     目录           0  2018-05-23 11:56  RAW\src\
     目录           0  2018-05-23 11:56  RAW\src\raw\
     文件        4527  2018-05-23 12:07  RAW\src\raw\RAW.java
     目录           0  2018-05-23 11:57  RAW\test\
     目录           0  2018-05-22 22:33  RW\
     文件        3591  2018-05-22 21:54  RW\build.xml
     目录           0  2018-05-22 22:33  RW\build\
     目录           0  2018-05-22 22:33  RW\build\classes\
     文件           0  2018-05-22 22:33  RW\build\classes\.netbeans_automatic_build
     文件           0  2018-05-22 22:33  RW\build\classes\.netbeans_update_resources
............此处省略48个文件信息

评论

共有 条评论