欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java 读写者问题 实现

程序员文章站 2022-07-05 09:04:23
...

所谓的读者写者问题是指保证一个Writer进程必须与其他进程互斥地访问共享对象地同步问题.其读写操作限制(包括读者优先和写者优先):

   1)--写互斥,即不能有两个写者同时进行写操作;

   2)--写互斥,即不能同时有一个进程在读而另一个进程在写;

   3)--读允许,即允许同时有多个进程在读同一个对象或文件.

 

package myprojects.rwproblem;
import java.awt.*;
import java.io.*;
import java.lang.*;
import java.util.*;
public class Rwproblem
{
         static final int MAX_THREAD_NUM=64;
         public static void main(String args[])
         {
             char test='0';
             do
             {            
                 System.out.println();
                 System.out.println("----------Demo of Reader and Writer Problem----------");
                 System.out.println("     1:Reader Priority.");
                 System.out.println("     2:Writer Priority.");
                 System.out.println("     3:Exit to Windows.");
                 try
                 {
                    test=(char)System.in.read();
                    System.in.read();
                    }catch(IOException e){}
              }while(test!='3'&&test!='1'&&test!='2');
                        if(test=='3')return;
                              Reader_Writer_Thread [] m_thread;
                              m_thread=new Reader_Writer_Thread[MAX_THREAD_NUM];
                              m_thread[0]=new Reader_Writer_Thread('r',1,3,5);
                              m_thread[1]=new Reader_Writer_Thread('w',2,4,5);
                              m_thread[2]=new Reader_Writer_Thread('r',3,5,2);
                              m_thread[3]=new Reader_Writer_Thread('r',4,6,5);
                              m_thread[4]=new Reader_Writer_Thread('w',5,5.1,3);
                              if(test=='2')
                              {
                                       m_thread[0].setpriority(2);
                                       System.out.println("   Demo of R_W Problem in Writer_Priority:");
                              }
                              else System.out.println("   Demo of R_W Problem in Reader_Priority:");
                              m_thread[0].start();
                     m_thread[1].start();
                     m_thread[2].start();
                     m_thread[3].start();
                     m_thread[4].start();
          }
}
class CriticalOfReader//读者临界区
{
         private int m_critical;
         CriticalOfReader()
         {
                   m_critical=0;
         }
         public synchronized void enterCriticalSection()//进入临界区
         {
                   try
                   {
                    if(m_critical!=0)wait();
                    }catch (InterruptedException e) { }
             m_critical=1;
         }
         public synchronized void leaveCriticalSection()//离开临界区
         {
                   m_critical=0;
                   notify();
         }
}
class CriticalOfWriter//写者临界区
{
         private  int m_critical;
         CriticalOfWriter()
         {
                   m_critical=0;
         }
         public synchronized void enterCriticalSection()//进入临界区
         {
                   try
                   {
                      if(m_critical!=0)wait();
                    }catch (InterruptedException e) { }
             m_critical=1;
         }
         public synchronized void leaveCriticalSection()//离开临界区
         {
                   m_critical=0;
                   notify();
         }
}
class Reader_Writer_Thread extends Thread
{
         static final int PER_SEC_DELAY=1000;//每秒运行数
         private static int m_ipriority;//优先级别(1代表读者优先,2代表写者优先)
         private static CriticalOfWriter m_criticalOfWriter;//写者临界区
         private static CriticalOfReader m_criticalOfReader;//读者临界区
         private static int m_iNumOfWriter;//写者总数
         private static int m_iNumOfReader;//读者总数
         private char m_chEntity;//线程类别
         private int m_iSerial;//***
         private double m_dDelay;//延迟时间
         private double m_dPersist;//读持续时间
         static
         {
                   m_ipriority=0;
                   m_iNumOfWriter=0;
                   m_iNumOfReader=0;
                   m_criticalOfWriter=new CriticalOfWriter();
                   m_criticalOfReader=new CriticalOfReader();
         }
         Reader_Writer_Thread(char entity,int serial,double delay,double persist)//构造函数
         {
                   m_chEntity=entity;
                   m_iSerial=serial;
                   m_dDelay=delay;
                   m_dPersist=persist;
         }
         public boolean setpriority(int priority)//设置优先数
         {
                   if(priority==1||priority==2){m_ipriority=priority;return true;}
                   return false;
         }
         public void run()//主运行
         {
                   //区别写者与读者线程
                   if(m_chEntity=='r')reader_run();
                   else if(m_chEntity=='w')writer_run();
         }
         public void reader_run()//读者线程运行函数
         {
                   try
                   {
                            Thread.sleep((long)m_dDelay*PER_SEC_DELAY);
                   }catch(InterruptedException e){}
                   Date temp=new Date();
                   System.out.print(Integer.toString(temp.getMinutes())+":"
                                         +Integer.toString(temp.getSeconds())+" ");
                   System.out.println("Reader thread "+m_iSerial+" sends the reading require.");
                   incNumOfReader();
                   try
                   {
                            sleep((long)m_dPersist*PER_SEC_DELAY);
                   }catch(InterruptedException e){}
                   decNumOfReader();
         }
         public void writer_run()//写者线程线程运行函数
         {
                   try
                   {
                            Thread.sleep((long)m_dDelay*PER_SEC_DELAY);
                   }catch(InterruptedException e){}
                   Date temp=new Date();
                   System.out.print(Integer.toString(temp.getMinutes())+":"
                                         +Integer.toString(temp.getSeconds())+" ");
                   System.out.println("Writer thread "+m_iSerial+" sends the writing require.");
                   incNumOfWriter();
                   try
                   {
                            sleep((long)m_dPersist*PER_SEC_DELAY);
                   }catch(InterruptedException e){}
                   decNumOfWriter();
         }
         public synchronized void incNumOfReader()//互斥
         {
                  
                   //写者优先时,先进入读者临界区
                   if(m_ipriority==2)m_criticalOfReader.enterCriticalSection();
                   //如果是第一个读者,则等待写者写完
                   m_iNumOfReader++;
                   if(m_iNumOfReader==1)m_criticalOfWriter.enterCriticalSection();
                  
                   if(m_ipriority==2)m_criticalOfReader.leaveCriticalSection();//让其他读者进
                   Date temp=new Date();
                   System.out.print(Integer.toString(temp.getMinutes())+":"
                                         +Integer.toString(temp.getSeconds())+" ");
                   System.out.println("Reader thread "+m_iSerial+" begins to read the file!");
         }
         public synchronized void decNumOfReader()//互斥
         {
                   m_iNumOfReader--;
                   Date temp=new Date();
                   System.out.print(Integer.toString(temp.getMinutes())+":"
                                         +Integer.toString(temp.getSeconds())+" ");
                   System.out.println("Reader thread "+m_iSerial+" finished reading the file!");
                   if(m_iNumOfReader==0)m_criticalOfWriter.leaveCriticalSection();//唤醒写者
         }
         public synchronized void incNumOfWriter()//互斥
         {
                   m_iNumOfWriter++;
                   //若是写者优先,第一个写者需等待读者写完
                   if(m_ipriority==2&&m_iNumOfWriter==1)m_criticalOfReader.enterCriticalSection();
                   m_criticalOfWriter.enterCriticalSection();//进入写者临界区
                   Date temp=new Date();
                   System.out.print(Integer.toString(temp.getMinutes())+":"
                                         +Integer.toString(temp.getSeconds())+" ");
                   System.out.println("Writer thread "+m_iSerial+" begins to write to the file!");
         }
         public synchronized void decNumOfWriter()//互斥
         {
                   m_iNumOfWriter--;
                   Date temp=new Date();
                   System.out.print(Integer.toString(temp.getMinutes())+":"
                                         +Integer.toString(temp.getSeconds())+" ");
                   System.out.println("Reader thread "+m_iSerial+" finished writing to the file!");
                   m_criticalOfWriter.leaveCriticalSection();//唤醒等待者(读者写者都有可能)
                   if(m_iNumOfWriter==0&&m_ipriority==2)m_criticalOfReader.leaveCriticalSection();
                   //最后一个写者,唤醒读者
         }
}