Below is the example code of the problem:We a have main thread and another thread which calls the enters the run function.
public class WhatDoesThisDo {
private static boolean ready;
private static int number;
private static class ReaderThread extends Thread {
public void run() {
while (!ready)
Thread.yield();
System.out.println(number);
}
}
public static void main(String[] args) {
new ReaderThread().start();
number = 42;
ready = true;
}
}
The correct answer according to some is that it either prints 42, 0 or nothing. Please explain why it can print nothing.