正在做一个读者写者问题的作业,要求读者写者公平竞争,我用了队列模拟FIFO的已达到公平竞争,但是在运行时候一直提示队列为空,查了好久,发现我的endReading和endWriting方法在读写完之后均无调用,请问是什么原因?谢谢
下面是我写的程序这个类是共用堆import java.util.Queue;
public class Puffer {
private int readerCount;//读者数
 private int writerCount;//写者数
 private boolean dbReading;//读信号量
 private boolean dbWriting;//写信号量
 private Queue queue;
 
 public Puffer() {
  readerCount=0;
  writerCount=0;
  dbReading=false;
  dbWriting=false;
 
 }
 public synchronized int startRead(Reader r){//开始读
  while(writerCount>0){
   try{
queue.add(r);
    System.out.println("reader is waiting");
    wait();//等待写者发出notify
   }
   catch(Exception e){
   }
  }
  readerCount++;
  if(readerCount==1){
  dbReading=true;
  }
  return readerCount;
 }
 
 public synchronized int endReading(){//结束读
  --readerCount;
  System.out.println("endReading"+queue.size());
  
  if(readerCount==0&&queue.size()!=0){
  dbReading=false;
 
  for(Object o:queue)
  {
  
  if(o.toString().equals("Reader"))
  {
  notifyAll();
  queue.remove();
  }else
  {
  notifyAll();
  break;
  }
  }//没有读者
   
  }

  //notifyAll();
  System.out.println("one reader is done reading. readerCount="+readerCount);
  return readerCount;
 }
 public synchronized void startWriting(Writer w){//开始写
  ++writerCount;
  while(dbReading==true||dbWriting==true){
   try{
queue.add(w);
    System.out.println("Writer "+writerCount+" is waiting");
    wait();//等待读者发出notify
   }
   catch(Exception e){
   }
  
  }
  dbWriting =true;
 }
 
 public synchronized void endWriting(){//结束写
  --writerCount;
  System.out.println("endWriting"+queue.size());
    if(writerCount==0&&queue.size()!=0){//没有写者
    dbWriting=false;
  
    for(Object o:queue)
  {
    
  if(o.toString().equals("Reader"))
  {
  queue.remove();
  notifyAll();
  }else
  {
  notifyAll();
  break;
  }
  }
  }
  //System.out.println("one writer is done writing. writerCount="+writerCount);
  
  //notifyAll();
 }
 
}

解决方案 »

  1.   

    读者类public class Reader extends Thread{
    private Puffer C;
     private int readerNum;
     
     public Reader(int r,Puffer db) {
      readerNum=r;
      C=db;
     }
     
     public void run(){
      int c;
      while(true){
      System.out.println("reader "+readerNum+" wants to read");
       c=C.startRead(this);
       System.out.println("reader "+readerNum+" is reading. readerCount="+c);
    //    try {
    // Thread.sleep(5);
    // } catch (InterruptedException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
       c=C.endReading();
       System.out.println("It is reader "+readerNum+" who has done reading. readercount="+c);
      
      }
    //   for ( int i = 0 ; i < 5 ; i++)
    //   {
    //    System.out.println("reader "+readerNum+" wants to read");
    //    c=C.startRead(this);  
    //    System.out.println("reader "+readerNum+" is reading. readerCount="+c);
    //    c=C.endReading();
    //    System.out.println("It is reader "+readerNum+" who has done reading. readercount="+c);
    //   
    //   }
     } @Override
    public String toString() {
    // TODO Auto-generated method stub
    return "Reader";
    }
     
    写者类
    public class Writer extends Thread{
    private Puffer C;
        private int writerNum;
     public Writer(int w,Puffer db) {
      writerNum=w;
      C=db;
     
     }
     
     public void run(){
      while(true)
     {
      System.out.println("Writer "+writerNum+" wants to write");
       C.startWriting(this);
     
       System.out.println("Writer "+writerNum+" is writing");
    //    try {
    // Thread.sleep(50);
    // } catch (InterruptedException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
       C.endWriting();
       System.out.println("It is Writer "+writerNum+" who has done writing .");
     }
    //   for ( int i = 0 ; i < 5 ; i++)
    // {
    //   
    //   System.out.println("Writer "+writerNum+" wants to write");
    //   C.startWriting(this);
    //   System.out.println("Writer "+writerNum+" is writing");
    //   C.endWriting();
    //   System.out.println("It is Writer "+writerNum+" who has done writing .");
    //  
    //  }
     
     }
    }
      

  2.   

    main方法
    public class Test {
    public static void main(String args[])
    {
    Puffer p=new Puffer();

      Reader r1=new Reader(1,p);//定义读者r1
      Reader r2=new Reader(2,p);//定义读者r2
      Reader r3=new Reader(3,p);
      Writer w1=new Writer(1,p);//定义写者w1
      Writer w2=new Writer(2,p);
     
      r1.start();
      w1.start();
      r2.start();
      w2.start();
      r3.start();
       
    }
    }