I am trying wait(), and notify() methods for multi-threading.
I have come up with a small project which I am trying to solve with multi-threading. I have a Taxi, that will reach Rank 6, and I have a Passenger that will reach Rank6.
Taxi will arrive at Rank 6 earlier that Passenger and will wait() for Passenger.
When Passenger reaches Rank6, he will notify().
After getting notified, that Taxi, will continue with the loop and will no to other Ranks.
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();
}
}
The output I am getting is:
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)
I need to know why the exception is getting thrown, to me it looks like that the Taxi thread is not getting back the lock. How should this scenario be implemented and how can Taxi thread continue with the loop.
Your help will really be appreciated.