Consider the following code: -
class CalculateSeries implements Runnable{
int total;
public void run(){
synchronized(this){
for(int i = 1; i <= 10000; i++) {
total += i;
}
notify();
}
}
}
Another instance expects an instance of this class, getting its lock inside the synchronized block. But if I do not save the code in the run method in a synchronized block, then I get IllegalMonitorStateException.
notify()should indicate a signal for all pending threads. Then why should it be inside a synchronized block?
source
share