java多线程饥饿现象的问题及解决办法(多线程面试题)
生活随笔
收集整理的這篇文章主要介紹了
java多线程饥饿现象的问题及解决办法(多线程面试题)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這篇文章主要介紹了java 多線程饑餓現象的問題解決方法的相關資料,需要的朋友可以參考下
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多线程饥饿现象的问题及解决办法(多线程面试题)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sap模块有哪些(的算法有哪些)
- 下一篇: oracle sga是什么