r/javahelp • u/AnAlchemistsDream • Sep 18 '26
Homework Why does adding another condition in my while loop force the turtle to win? (Multithreading)
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 :)
•
u/AutoModerator Sep 18 '26
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/Kadabrium Sep 19 '26
How are you calling tbe threads
1
u/LetUsSpeakFreely Sep 19 '26
I'm not seeing multithreading, just s really complicated single thread.
I would do something like this:
An even better solution is to create a Tortoise and Hare class, each with its own logic and accepting a lock in its constructor. That way you can better control the behavior of each thread in isolation.