2013-10-17 65 views
0

我正在嘗試wait()notify()用於多線程的方法。在多線程中使用wait()和notify()

我想出了一個小型項目,我試圖用多線程解決。 我有一輛出租車,將達到6級,我有一個乘客,將達到Rank6。

出租車將到達排名第6的乘客,並將爲wait()乘客。 當乘客到達Rank6時,他會notify()

在得到通知後,該出租車將繼續循環,並且不會向其他等級。

Taxi.java

package multhithreading.engage.hireForHier; 

public class Taxi implements Runnable { 

Rank rank = null; 

public Taxi(Rank rank) { 
    this.rank = rank; 
} 

public void run() { 
    System.out.println(Thread.currentThread().getName() + " Destined for rank No. " + rank.getRankNo()); 
    synchronized (rank) { 

     for (int i = 1; i < 10; i++) { 
      System.out.println("Taxi has reached rank: " + i); 
      if (i == 6) { 
       try { 
        rank.wait(); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       }// catch 
      }// if 

      try { 
       Thread.sleep(100); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      }// catch 

     }// for 
    }// synchronized 

}// run 

} 

Passenger.java

package multhithreading.engage.hireForHier; 

public class Passenger implements Runnable { 

Rank rank = null; 

public Passenger(Rank rank) { 
    this.rank = rank; 
} 

public void run() { 
    System.out.println(Thread.currentThread().getName() + " Destined for rank No. " + rank.getRankNo()); 

    synchronized (rank) { 

     for (int i = 1; i < 10; i++) { 

      System.out.println("Passenger has reached rank: " + i); 
      if (i == 6) { 
       notify(); 

      }// if 
      try { 
       Thread.sleep(180); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      }// catch 

     }// for 
    }// synchronized 

}// run 
} 

Rank.java

package multhithreading.engage.hireForHier; 


public class Rank { 

private int rankNo = 0; 

public Rank(int rankNo) { 
    this.rankNo = rankNo; 
} 

public int getRankNo() { 
    return rankNo; 
} 
} 

TaxiHire.java

package multhithreading.engage.hireForHier; 

public class TaxiHire { 

public static void main(String[] args) { 

    Rank rank6 = new Rank(6); 

    Taxi taxiNo3 = new Taxi(rank6); 
    Passenger passengerNo3 = new Passenger(rank6); 

    Thread taxi_thread = new Thread(taxiNo3, "taxi_thread"); 

    Thread passenger_thread = new Thread(passengerNo3, "passenger_thread"); 

    taxi_thread.start(); 
    passenger_thread.start(); 
} 

} 

我得到的輸出是:

taxi_thread Destined for rank No. 6 
Taxi has reached rank: 1 
passenger_thread Destined for rank No. 6 
Taxi has reached rank: 2 
Taxi has reached rank: 3 
Taxi has reached rank: 4 
Taxi has reached rank: 5 
Taxi has reached rank: 6 
Passenger has reached rank: 1 
Passenger has reached rank: 2 
Passenger has reached rank: 3 
Passenger has reached rank: 4 
Passenger has reached rank: 5 
Passenger has reached rank: 6 
Exception in thread "passenger_thread" java.lang.IllegalMonitorStateException 
    at java.lang.Object.notify(Native Method) 
    at lesson9.engage.hireForHier.Passenger.run(Passenger.java:20) 
    at java.lang.Thread.run(Unknown Source) 

我需要知道爲什麼異常被拋出越來越對我來說,它看起來像出租車線程沒有找回鎖。 這個場景應該如何實現以及Taxi線程如何繼續循環。

您的幫助將真正被讚賞。

回答

1

的錯誤是在客運班,您同步蘭特對象,但通知這個對象 所以你的代碼是

if (i == 6) { 
       notify(); 

      }// if 

,並應

if (i == 6) { 
       try{rank.notify(); 
       }catch(Exception ex){/*When no any thread is waiting for rank object.*/} 

      }// if 

也是這可能是出租車線程去首先,在這裏你將面臨異常,因爲沒有任何線程正在等待排名對象。

所以先啓動出租車線程,然後啓動乘客,最好的方法是用出租車線程調用乘客線程。它確保您的出租車線程先行。

這個link可能會幫助你可能的情況。

+0

感謝user2511414,這是非常有幫助的。 – user2106149

+0

我很高興我可以給一些手:)有一個很好的並行應用程序。 – 2013-10-17 20:45:51

1
if (i == 6) { 
    notify(); 

在這裏,你是不是要求rank.notify(),所以默認行爲將嘗試電話通知上this對象Passenger對象),因爲你不上持有鎖,將拋出IllegalMonitorStateExceptionthis

IllegalMonitorStateException發生在您嘗試對某個對象執行wait()notify()notifyAll() 時未鎖定該對象。

解決方案:

if (i == 6) { 
    rank.notify(); 
+0

謝謝Narendra,這非常有幫助。 – user2106149

相關問題