溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

java項(xiàng)目中的多線程出現(xiàn)饑餓現(xiàn)象如何解決

發(fā)布時(shí)間:2020-11-23 16:55:42 來(lái)源:億速云 閱讀:345 作者:Leah 欄目:編程語(yǔ)言

java項(xiàng)目中的多線程出現(xiàn)饑餓現(xiàn)象如何解決?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

java 多線程饑餓現(xiàn)象的問(wèn)題解決方法

當(dāng)有線程正在讀的時(shí)候,不允許寫 線程寫,但是允許其他的讀線程進(jìn)行讀。有寫線程正在寫的時(shí)候,其他的線程不應(yīng)該讀寫。為了防止寫線程出現(xiàn)饑餓現(xiàn)象,當(dāng)線程正在讀,如果寫線程請(qǐng)求寫,那么應(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)線程正在寫或者 
                                        //有線程正在等待寫,則禁止讀線程繼續(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)線程正在被讀或者被寫或者有線程等待讀 
       
        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.... 

關(guān)于java項(xiàng)目中的多線程出現(xiàn)饑餓現(xiàn)象如何解決問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI