public class Reader extends Thread{ Calculator c;
public Reader(Calculator cal) {
c=cal;
}
public void run()
{
synchronized(c)
{
try
{
System.out.println(""+Thread.currentThread().getName()+" waiting for calculation");
c.wait();
}
catch(InterruptedException e)
{
}
System.out.println("Total="+c.total);
}
}
public static void main(String args[])
{
Calculator C=new Calculator();
new Reader(C).start();
new Reader(C).start();
new Reader(C).start();
C.start();
}
} class Calculator extends Thread { int total;
public void run()
{
synchronized(this)
{
for(int i=0;i<100;i++)
total+=i;
}
notifyAll();//notifyAll();
}
}