i'm currently learning about multithreading for my java class and part of the assignment requires me to race two threads (turtle and a hare) together and force one of them to win using the wait() and notify() methods (in this case the turtle thread).
public class Race {
private Integer numberOfLaps;
private Integer lapToStart = 2;
private boolean isWinning = true;
public synchronized void winner(Integer numberOfLaps) {
while (!isWinning) {
try {
wait();
}
catch (InterruptedException e){}
}
this.numberOfLaps = numberOfLaps;
System.
out
.println("Tortoise is on lap: " + numberOfLaps);
isWinning = false;
notify();
}
public synchronized void loser() {
while (isWinning && numberOfLaps < lapToStart) {
try {
wait();
}
catch (InterruptedException e){}
}
System.
out
.println("Hare is on lap: " + numberOfLaps);
isWinning = true;
notify();
}
} public class Race {
private Integer numberOfLaps;
private Integer lapToStart = 2;
private boolean isWinning = true;
public synchronized void winner(Integer numberOfLaps) {
while (!isWinning) {
try {
wait();
}
catch (InterruptedException e){}
}
this.numberOfLaps = numberOfLaps;
System.out.println("Tortoise is on lap: " + numberOfLaps);
isWinning = false;
notify();
}
public synchronized void loser() {
while (isWinning) {
try {
wait();
}
catch (InterruptedException e){}
}
System.out.println("Hare is on lap: " + numberOfLaps);
isWinning = true;
notify();
}
}Output:
Tortoise is on lap: 1
Hare is on lap: 1
Tortoise is on lap: 2
Hare is on lap: 2
Tortoise is on lap: 3
Hare is on lap: 3
Tortoise is on lap: 4
Hare is on lap: 4
Tortoise is on lap: 5
Hare is on lap: 5
Tortoise is on lap: 6
Hare is on lap: 6
Tortoise is on lap: 7
Hare is on lap: 7
Tortoise is on lap: 8
Hare is on lap: 8
Tortoise is on lap: 9
Hare is on lap: 9
Tortoise is on lap: 10
Hare is on lap: 10
If this prints out first before the hare finishes, the turtle has won! Congrats
If in the while loop for the loser method is only the volatile boolean, both the turtle and the hare finish at the exact same time. however, if i add another condition to it
public synchronized void loser() {
while (isWinning && numberOfLaps < lapToStart) {
try {
wait();
}
catch (InterruptedException e){}
}public synchronized void loser() {
while (isWinning && numberOfLaps < lapToStart) {
try {
wait();
}
catch (InterruptedException e){}
}the output becomes:
Tortoise is on lap: 1
Hare is on lap: 1
Tortoise is on lap: 2
Hare is on lap: 2
Tortoise is on lap: 3
Hare is on lap: 3
Hare is on lap: 3
Tortoise is on lap: 4
Hare is on lap: 4
Tortoise is on lap: 5
Hare is on lap: 5
Tortoise is on lap: 6
Hare is on lap: 6
Tortoise is on lap: 7
Hare is on lap: 7
Tortoise is on lap: 8
Hare is on lap: 8
Tortoise is on lap: 9
Hare is on lap: 9
Tortoise is on lap: 10
If this prints out first before the hare finishes, the turtle has won! Congrats
now whenever the code is ran, the hare always lags behind at some point and the turtle finishes first. why does the code suddenly function as i intended when i add a second condition to the while loop? also, this is my first time posting something code related. i apologize if i'm missing anything. any help would be appreciated. thank you :)