Dear Friends...!
What will happen if we use a wait() without having notify() call in the programme.
According to the theory the
thread waiting should wait forever until another
thread notifies on that object, which the previous thread is waiting for .
But in the following programme I coudn't see any differenecs in the output
when I use wait() with and without notify();
Please tell me why ?
Here is the code...!!!
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(e);}
}
System.out.println("Total is: " + c.total);
}
public static void main(
String [] args) {
Calculator calculator = new Calculator();
new Reader(calculator).start();
new Reader(calculator).start();
new Reader(calculator).start();
calculator.start();
}
}
class Calculator extends Thread {
int total;
public void run() {
synchronized(this) {
for(int i=0;i<100;i++) {
total += i;
}
// notify();
}