I am trying to understand below program. If I call new ReaderThread().start()
it is working fine, but if I call new ReaderThread().run()
, the application goes into an infinite loop. What is the difference?
public class Contention {
private static boolean ready;
private static int number;
private static class ReaderThread extends Thread {
public void run() {
while (!ready){
System.out.println("ready ..."+ready);
Thread.yield();}
System.out.println(number);
// }
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new ReaderThread().run();
number = 42;
ready = true;
}
}