I had a small project lately, where I had to do something over a while loop and have a stall in the while loop that will be stopped by a Thread.
Since my project is huge, I won't be able to share something that others can test and experience. Therefore, I made a small example about that.
This is the stall I am creating in the while loop, then the thread comes up and stops it.
Once I don't print anything in the while loop, the while loop is infinite. Once I print something in the while loop, anything, the while loop is finite.
static boolean stallPipeline = false;
public static void main (String[] args) {
int i = 0;
do {
while (stallPipeline) {
//System.out.println("");
}
stallPipeline = true;
new Thread(() -> {
try {
Thread.sleep(10);
} catch (Exception e) {}
stallPipeline = false;
}).start();
} while (i++ < 2);
}
Once you uncomment the System.out.println();
, the loop will no longer be infinite.
Can anyone explain that?