java 多線程饑餓現(xiàn)象的問(wèn)題解決方法
java 多線程饑餓現(xiàn)象的問(wèn)題解決方法
當(dāng)有線程正在讀的時(shí)候,不允許寫(xiě) 線程寫(xiě),但是允許其他的讀線程進(jìn)行讀。有寫(xiě)線程正在寫(xiě)的時(shí)候,其他的線程不應(yīng)該讀寫(xiě)。為了防止寫(xiě)線程出現(xiàn)饑餓現(xiàn)象,當(dāng)線程正在讀,如果寫(xiě)線程請(qǐng)求寫(xiě),那么應(yīng)該禁止再來(lái)的讀線程進(jìn)行讀。
實(shí)現(xiàn)代碼如下:
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)//當(dāng)線程正在寫(xiě)或者
//有線程正在等待寫(xiě),則禁止讀線程繼續(xù)讀
{
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);
//當(dāng)線程正在被讀或者被寫(xiě)或者有線程等待讀
while(pool.getWriterNumber()>0 || pool.getReaderNumber()>0)
{
try {
pool.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
pool.setWaittingWriten(false); //這個(gè)策略還算公平
{
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();
}
}
}
程序部分運(yùn)行結(jié)果如下:
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....
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
基于Protobuf動(dòng)態(tài)解析在Java中的應(yīng)用 包含例子程序
下面小編就為大家?guī)?lái)一篇基于Protobuf動(dòng)態(tài)解析在Java中的應(yīng)用 包含例子程序。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
rocketmq client 日志的問(wèn)題處理方式
這篇文章主要介紹了rocketmq client 日志的問(wèn)題處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
用Rational Rose逆向工程(java)生成類圖(教程和錯(cuò)誤解決)
Rational Rose有個(gè)很方便的功能,將項(xiàng)目中的JAVA代碼自動(dòng)轉(zhuǎn)換成UML類圖2013-02-02
Java實(shí)現(xiàn)DES加密與解密,md5加密以及Java實(shí)現(xiàn)MD5加密解密類
這篇文章主要介紹了Java實(shí)現(xiàn)DES加密與解密,md5加密以及Java實(shí)現(xiàn)MD5加密解密類 ,需要的朋友可以參考下2015-11-11
解決子線程中獲取不到HttpServletRequest對(duì)象的問(wèn)題
這篇文章主要介紹了解決子線程中獲取不到HttpServletRequest對(duì)象的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
基于Springboot執(zhí)行多個(gè)定時(shí)任務(wù)并動(dòng)態(tài)獲取定時(shí)任務(wù)信息
這篇文章主要為大家詳細(xì)介紹了基于Springboot執(zhí)行多個(gè)定時(shí)任務(wù)并動(dòng)態(tài)獲取定時(shí)任務(wù)信息,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
IDEA部署Docker鏡像的實(shí)現(xiàn)示例
本文主要介紹了IDEA部署Docker鏡像的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
java調(diào)用js文件的兩種常用方法示例(支持V8引擎)
在Java中調(diào)用JavaScript的方法通常涉及到使用Java的腳本引擎,下面這篇文章主要給大家介紹了關(guān)于java調(diào)用js文件的兩種常用方法(支持V8引擎)的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06
解決Springboot中Feignclient調(diào)用時(shí)版本問(wèn)題
這篇文章主要介紹了解決Springboot中Feign?client調(diào)用時(shí)版本問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03

