java项目中的多线程出现饥饿现象如何解决
java项目中的多线程出现饥饿现象如何解决?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
创新互联公司拥有10余年的建站服务经验,在此期间,我们发现较多的客户在挑选建站服务商前都非常的犹豫。主要问题集中:在无法预知自己的网站呈现的效果是什么样的?也无法判断选择的服务商设计出来的网页效果自己是否会满意?创新互联公司业务涵盖了互联网平台网站建设、移动平台网站制作、网络推广、按需定制设计等服务。创新互联公司网站开发公司本着不拘一格的网站视觉设计和网站开发技术相结合,为企业做网站提供成熟的网站设计方案。
java 多线程饥饿现象的问题解决方法
当有线程正在读的时候,不允许写 线程写,但是允许其他的读线程进行读。有写线程正在写的时候,其他的线程不应该读写。为了防止写线程出现饥饿现象,当线程正在读,如果写线程请求写,那么应该禁止再来的读线程进行读。
实现代码如下:
File.Java
package readerWriter; public class File { private String name; public File(String 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 setWaittingWriten(boolean waittingWriten) { this.waittingWriten = waittingWriten; } public File getFile() { return file; } public void setFile(File file) { this.file = file; } File file; public Pool(File file) { this.file=file; } public int getReaderNumber() { return readerNumber; } public void setReaderNumber(int readerNumber) { this.readerNumber = readerNumber; } public int getWriterNumber() { return writerNumber; } public void setWriterNumber(int writerNumber) { this.writerNumber = writerNumber; } }
Reader.java
package readerWriter; public class Reader implements Runnable{ private String id; private Pool pool; public Reader(String id,Pool pool) { this.id=id; this.pool=pool; } @Override public void run() { // TODO Auto-generated method stub while(!Thread.currentThread().interrupted()){ synchronized(pool){ while(pool.getWriterNumber()>0 || pool.isWaittingWriten()==true)//当线程正在写或者 //有线程正在等待写,则禁止读线程继续读 { try { pool.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } { pool.setReaderNumber(pool.getReaderNumber()+1); } } System.out.println(id+" "+"is reading...."); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } synchronized(pool) { pool.setReaderNumber(pool.getReaderNumber()-1); System.out.println(id+" "+"is existing the reader...."); if(pool.getReaderNumber()==0) pool.notifyAll(); } try { Thread.sleep(1000); } 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 Writer(String id,Pool pool) { this.id=id; this.pool=pool; } @Override public void run() { // TODO Auto-generated method stub 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 (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } pool.setWaittingWriten(false); //这个策略还算公平 { pool.setWriterNumber(pool.getWriterNumber()+1); } } System.out.println(id+" "+"is writing...."); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // synchronized(pool) { pool.setWriterNumber(pool.getWriterNumber()-1); System.out.println(id+" "+"is existing the writer...."); pool.notifyAll(); } /* try { Thread.sleep(1000); //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 main(String[] args) { // TODO Auto-generated method stub Pool pool=new Pool(new File("dd file")); for(int i=0;i<2;i++) { Thread writer=new Thread(new Writer("writer "+i,pool)); writer.start(); } for(int i=0;i<5;i++) { Thread reader=new Thread(new 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项目中的多线程出现饥饿现象如何解决问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。
标题名称:java项目中的多线程出现饥饿现象如何解决
转载来于:http://scgulin.cn/article/gchoog.html