running 3 threads in sequence java

Convert those IF statements to WHILE statements to get the desired behavior:

if (notifyAllExample.status != 2){
    notifyAllExample.wait();
}

to

while (notifyAllExample.status != 2){
    notifyAllExample.wait();
}

This will ensure that if a thread is notified, it won’t go out of the while loop until the status value is what it expects.

Also, mark status as volatile so that the threads won’t have a local copy.

Leave a Comment