这篇文章主要介绍了java 多线程饥饿现象的问题解决方法的相关资料,需要的朋友可以参考下
java 多线程饥饿现象的问题解决方法
当有线程正在读的时候,不允许写 线程写,但是允许其他的读线程进行读。有写线程正在写的时候,其他的线程不应该读写。为了防止写线程出现饥饿现象,当线程正在读,如果写线程请求写,那么应该禁止再来的读线程进行读。
实现代码如下:
File.Java
package readerWriter; public class File { private String name; public FileString name) { this.name=name; } }
Pool.java
package readerWriter; public class Pool { private int readerNumber=0; private int writerNumber=0; private boolean waittingWriten; public boolean isWaittingWriten) { return waittingWriten; } public void setWaittingWritenboolean waittingWriten) { this.waittingWriten = waittingWriten; } public File getFile) { return file; } public void setFileFile file) { this.file = file; } File file; public PoolFile file) { this.file=file; } public int getReaderNumber) { return readerNumber; } public void setReaderNumberint readerNumber) { this.readerNumber = readerNumber; } public int getWriterNumber) { return writerNumber; } public void setWriterNumberint writerNumber) { this.writerNumber = writerNumber; } }
Reader.java
package readerWriter; public class Reader implements Runnable{ private String id; private Pool pool; public ReaderString id,Pool pool) { this.id=id; this.pool=pool; } @Override public void run) { // TODO Auto-generated method stub while!Thread.currentThread).interrupted)){ synchronizedpool){ whilepool.getWriterNumber)>0 || pool.isWaittingWriten)==true)//当线程正在写或者 //有线程正在等待写,则禁止读线程继续读 { try { pool.wait); } catch InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace); } } { pool.setReaderNumberpool.getReaderNumber)+1); } } System.out.printlnid+" "+"is reading...."); try { Thread.sleep1000); } catch InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace); } synchronizedpool) { pool.setReaderNumberpool.getReaderNumber)-1); System.out.printlnid+" "+"is existing the reader...."); ifpool.getReaderNumber)==0) pool.notifyAll); } try { Thread.sleep1000); } catch InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace); } // pool.notifyAll); } } }
Writer.java
package readerWriter; public class Writer implements Runnable{ private Pool pool; String id; public WriterString id,Pool pool) { this.id=id; this.pool=pool; } @Override public void run) { // TODO Auto-generated method stub while!Thread.currentThread).interrupted)){ synchronizedpool){ ifpool.getReaderNumber)>0) pool.setWaittingWritentrue); else pool.setWaittingWritenfalse); //当线程正在被读或者被写或者有线程等待读 whilepool.getWriterNumber)>0 || pool.getReaderNumber)>0) { try { pool.wait); } catch InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace); } } pool.setWaittingWritenfalse); //这个策略还算公平 { pool.setWriterNumberpool.getWriterNumber)+1); } } System.out.printlnid+" "+"is writing...."); try { Thread.sleep1000); } catch InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace); } // synchronizedpool) { pool.setWriterNumberpool.getWriterNumber)-1); System.out.printlnid+" "+"is existing the writer...."); pool.notifyAll); } /* try { Thread.sleep1000); //System.out.println"writer sleeping over"); } catch InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace); } */ } } }
Main.java
package readerWriter; public class Main { public static void mainString[] args) { // TODO Auto-generated method stub Pool pool=new Poolnew File"dd file")); forint i=0;i<2;i++) { Thread writer=new Threadnew Writer"writer "+i,pool)); writer.start); } forint i=0;i<5;i++) { Thread reader=new Threadnew Reader"reader "+i,pool)); reader.start); } } }
程序部分运行结果如下:
writer 0 is writing.... writer 0 is existing the writer.... writer 0 is writing.... writer 0 is existing the writer.... writer 0 is writing.... writer 0 is existing the writer.... writer 0 is writing.... writer 0 is existing the writer.... writer 1 is writing.... writer 1 is existing the writer.... writer 1 is writing.... writer 1 is existing the writer.... writer 1 is writing.... writer 1 is existing the writer.... writer 1 is writing.... writer 1 is existing the writer.... writer 0 is writing.... writer 0 is existing the writer.... writer 0 is writing.... writer 0 is existing the writer.... reader 0 is reading.... reader 0 is existing the reader.... writer 1 is writing.... writer 1 is existing the writer.... writer 1 is writing.... writer 1 is existing the writer.... writer 1 is writing.... writer 1 is existing the writer.... writer 1 is writing.... writer 1 is existing the writer.... writer 1 is writing.... writer 1 is existing the writer.... writer 0 is writing.... writer 0 is existing the writer.... writer 0 is writing.... writer 0 is existing the writer.... writer 0 is writing.... writer 0 is existing the writer.... writer 0 is writing.... writer 0 is existing the writer.... reader 3 is reading.... reader 2 is reading.... reader 4 is reading.... reader 1 is reading.... reader 0 is reading.... reader 3 is existing the reader.... reader 1 is existing the reader.... reader 0 is existing the reader.... reader 4 is existing the reader.... reader 2 is existing the reader.... writer 0 is writing.... writer 0 is existing the writer.... writer 0 is writing.... writer 0 is existing the writer.... writer 1 is writing.... writer 1 is existing the writer.... reader 2 is reading.... reader 2 is existing the reader.... writer 0 is writing.... writer 0 is existing the writer.... writer 0 is writing.... writer 0 is existing the writer.... writer 0 is writing....
以上就是java多线程饥饿现象的问题及解决办法的详细内容,更多请关注风君子博客其它相关文章!