Given a thread has multiple states: alive runnable running waiting and terminated. the notifyAll() method is suppose to put all threads that are "waiting" on an object's lock back into the "runnable" state where it is may be chosen as the next running object. The following example instantiates and starts 3 Reader threads, which go into waiting (wait() method) until the 'calc' object's lock is released. The calc objects thread is instantiated and started just after this where it adds up some numbers, followed by notifyAll().
My question is, why doesn't the calc thread notify all the Reader threads every time? when I run this on my computer it's hit and miss.
public class Reader extends Thread{
Calculator c;
public Reader(Calculator calc){
c=calc;
}
public void run(){
synchronized(c){
try{
System.out.println("Waiting for calculation...");
c.wait();
}catch(InterruptedException e){}
System.out.println("Total is: "+c.total);
}
}
public static void main(String[] args){
Calculator calc = new Calculator();
new Reader(calc).start();
new Reader(calc).start();
new Reader(calc).start();
new Thread(calc).start();
}
}
class Calculator implements Runnable{
int total;
public void run(){
synchronized(this){
for(int i =0; i<100; i++){
total+=i;
}
notifyAll();
}
}
}