public class SyncThread3 extends Thread {
Calculator c;
public SyncThread3(Calculator c)
{
this.c = c;
}
public void run()
{
synchronized(c)
{
try
{
System.out.println(Thread.currentThread().getName() +" wait calculate result");
c.wait();
}
catch(InterruptedException e)
{}
System.out.println(Thread.currentThread().getName() + " the result is " + c.total);
}
}
public SyncThread3()
{
Calculator calculator = new Calculator();
new SyncThread3(calculator).start();
new SyncThread3(calculator).start();
new SyncThread3(calculator).start();
calculator.start();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new SyncThread3();
}
public class Calculator extends Thread
{
int total;
public void run()
{
synchronized(this)
{
for (int i = 0 ; i < 101 ; i ++)
{
total += i;
}
}
notifyAll();
}
}
}