I want to use multithreading (low -level threading) but i am running into a problem. The problem is that because of at least wait method will be called by one thread and notifyAll will be called by another thread the problem is that any time i run the program it seems to me that notifyAll is called before wait so i "will wait forever".

My code is as below :

```
public class Reader extends Thread {

    Calculator c;

    public Reader(Calculator cal) {
        c=cal;
    }

    public void run (){
        synchronized(c) {
            try {
                System.out.println("Waiting for calculation");
                c.wait();
            } catch (InterruptedException ex) {}
            System.out.println("Total is:" +c.total);      
        }       
    }  

    public static void main (String[] a) {

        Calculator calculator=new Calculator();

        new Reader(calculator).start();
        new Reader(calculator).start();
        new Reader(calculator).start();
        new Reader(calculator).start();
        new Reader(calculator).start();
    }
}

class Calculator implements Runnable {

    int total;

    public void run()  {
        synchronized(this) {    
            for(int i=0;i<100;i++) {
                total=total+i;
            }       
            notifyAll();    
        }
    }
}

```

What i get here as output is 5 times in a row waiting for calculation so i never reach the statement "the total is the total.

I am trying to figure it out how to solve the problem but still not finding a solution. If anybody has any clue what to do i will appreciate a lot.

Thanks in advance