java 多线程饥饿现象的问题解决方法
java多线程饥饿现象的问题解决方法
当有线程正在读的时候,不允许写线程写,但是允许其他的读线程进行读。有写线程正在写的时候,其他的线程不应该读写。为了防止写线程出现饥饿现象,当线程正在读,如果写线程请求写,那么应该禁止再来的读线程进行读。
实现代码如下:
File.Java
packagereaderWriter; publicclassFile{ privateStringname; publicFile(Stringname) { this.name=name; } }
Pool.java
packagereaderWriter; publicclassPool{ privateintreaderNumber=0; privateintwriterNumber=0; privatebooleanwaittingWriten; publicbooleanisWaittingWriten(){ returnwaittingWriten; } publicvoidsetWaittingWriten(booleanwaittingWriten){ this.waittingWriten=waittingWriten; } publicFilegetFile(){ returnfile; } publicvoidsetFile(Filefile){ this.file=file; } Filefile; publicPool(Filefile) { this.file=file; } publicintgetReaderNumber(){ returnreaderNumber; } publicvoidsetReaderNumber(intreaderNumber){ this.readerNumber=readerNumber; } publicintgetWriterNumber(){ returnwriterNumber; } publicvoidsetWriterNumber(intwriterNumber){ this.writerNumber=writerNumber; } }
Reader.java
packagereaderWriter; publicclassReaderimplementsRunnable{ privateStringid; privatePoolpool; publicReader(Stringid,Poolpool) { this.id=id; this.pool=pool; } @Override publicvoidrun() { //TODOAuto-generatedmethodstub while(!Thread.currentThread().interrupted()){ synchronized(pool){ while(pool.getWriterNumber()>0||pool.isWaittingWriten()==true)//当线程正在写或者 //有线程正在等待写,则禁止读线程继续读 { try{ pool.wait(); }catch(InterruptedExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } { pool.setReaderNumber(pool.getReaderNumber()+1); } } System.out.println(id+""+"isreading...."); try{ Thread.sleep(1000); }catch(InterruptedExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } synchronized(pool) { pool.setReaderNumber(pool.getReaderNumber()-1); System.out.println(id+""+"isexistingthereader...."); if(pool.getReaderNumber()==0) pool.notifyAll(); }try{ Thread.sleep(1000); }catch(InterruptedExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } //pool.notifyAll(); } } }
Writer.java
packagereaderWriter; publicclassWriterimplementsRunnable{ privatePoolpool; Stringid; publicWriter(Stringid,Poolpool) { this.id=id; this.pool=pool; } @Override publicvoidrun(){ //TODOAuto-generatedmethodstub while(!Thread.currentThread().interrupted()){ synchronized(pool){ if(pool.getReaderNumber()>0) pool.setWaittingWriten(true); else pool.setWaittingWriten(false); //当线程正在被读或者被写或者有线程等待读 while(pool.getWriterNumber()>0||pool.getReaderNumber()>0) { try{ pool.wait(); }catch(InterruptedExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } pool.setWaittingWriten(false);//这个策略还算公平 { pool.setWriterNumber(pool.getWriterNumber()+1); } } System.out.println(id+""+"iswriting...."); try{ Thread.sleep(1000); }catch(InterruptedExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } // synchronized(pool) { pool.setWriterNumber(pool.getWriterNumber()-1); System.out.println(id+""+"isexistingthewriter...."); pool.notifyAll(); } /*try{ Thread.sleep(1000); //System.out.println("writersleepingover"); }catch(InterruptedExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); }*/ } } }
Main.java
packagereaderWriter; publicclassMain{ publicstaticvoidmain(String[]args){ //TODOAuto-generatedmethodstub Poolpool=newPool(newFile("ddfile")); for(inti=0;i<2;i++) { Threadwriter=newThread(newWriter("writer"+i,pool)); writer.start(); } for(inti=0;i<5;i++) { Threadreader=newThread(newReader("reader"+i,pool)); reader.start(); } } }
程序部分运行结果如下:
writer0iswriting.... writer0isexistingthewriter.... writer0iswriting.... writer0isexistingthewriter.... writer0iswriting.... writer0isexistingthewriter.... writer0iswriting.... writer0isexistingthewriter.... writer1iswriting.... writer1isexistingthewriter.... writer1iswriting.... writer1isexistingthewriter.... writer1iswriting.... writer1isexistingthewriter.... writer1iswriting.... writer1isexistingthewriter.... writer0iswriting.... writer0isexistingthewriter.... writer0iswriting.... writer0isexistingthewriter.... reader0isreading.... reader0isexistingthereader.... writer1iswriting.... writer1isexistingthewriter.... writer1iswriting.... writer1isexistingthewriter.... writer1iswriting.... writer1isexistingthewriter.... writer1iswriting.... writer1isexistingthewriter.... writer1iswriting.... writer1isexistingthewriter.... writer0iswriting.... writer0isexistingthewriter.... writer0iswriting.... writer0isexistingthewriter.... writer0iswriting.... writer0isexistingthewriter.... writer0iswriting.... writer0isexistingthewriter.... reader3isreading.... reader2isreading.... reader4isreading.... reader1isreading.... reader0isreading.... reader3isexistingthereader.... reader1isexistingthereader.... reader0isexistingthereader.... reader4isexistingthereader.... reader2isexistingthereader.... writer0iswriting.... writer0isexistingthewriter.... writer0iswriting.... writer0isexistingthewriter.... writer1iswriting.... writer1isexistingthewriter.... reader2isreading.... reader2isexistingthereader.... writer0iswriting.... writer0isexistingthewriter.... writer0iswriting.... writer0isexistingthewriter.... writer0iswriting....
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!