posted 20 years ago
ASSUME :
ThreadA, ThreadB and ThreadC are Reader Threads.
CalculatorThread and CalThread are Calculator Threads.
Case I
ThreadA, ThreadB, ThreadC all wait on CalculatorThread to finish a certain task.
CalculatorThread has a notify() call inside it’s run() just after the expected task of Reader threads completed. But Calculator thread can run beyond that.
CalculatorThread is still alive (not dead) when ThreaA,ThreadB,ThreadC hit their wait() call.
1)Only one Reader thread will be notified.(say ThreadB)
2)It will resume operations as soon as CalculatorThread notified and it exits the synchronized block, where “notify()” resides.
3)Other Reader threads will wait(). But now their wait() method behaves as the yield() method. ( Exactly as yield. It won’t release any locks it has now. But remember it has already released the lock on CalculatorThreard object, at the time it hit the wait() call)
4)So as soon as CalculatorThread exits it’s run() method, ThreadA & ThreadC resume their operaions.
Case I I
ThreadA, ThreadB, ThreadC all wait on CalculatorThread to finish a certain task.
CalculatorThread has a notify() ( or may not have a notify()) call inside it’s run() just after the expected task of Reader threads completed. But Calculator thread can run beyond that.
CalculatorThread is not alive (dead) when ThreaA,ThreadB,ThreadC hit their wait() call.
1)NO Reader thread will be notified.
2)All reader threads will wait(). But now their wait() method behaves as the sleep() method. ( Exactly as sleep(). It won’t release any locks it has now. But remember it has already released the lock on CalculatorThreard object, at the time it hit the wait() call)
Case III
ThreadA, ThreadB, ThreadC all wait on CalculatorThread to finish a certain task.
CalculatorThread has a notify() ( or may not have a notify()) call inside it’s run() just after the expected task of Reader threads completed. But Calculator thread can run beyond that.
CalculatorThread is not alive (dead) when ThreaA,ThreadB,ThreadC hit their wait() call.
But CalThread is alive, and contains a notify() ( or may not have a notify())call inside it’s run() just after the expected task of Reader threads completed.
1)NO Reader thread will be notified, because none of them waits on CalThread object.
2)All reader threads will wait(). But now their wait() method behaves as the sleep() method. ( Exactly as sleep(). It won’t release any locks it has now. But remember it has already released the lock on CalculatorThreard object, at the time it hit the wait() call)
Case I V
ThreadA, ThreadB, ThreadC all wait on CalculatorThread to finish a certain task.
CalculatorThread has a notifyAll() call inside it’s run() just after the expected task of Reader threads completed. But Calculator thread can run beyond that.
CalculatorThread is still alive (not dead) when ThreaA,ThreadB,ThreadC hit their wait() call.
1)All Reader threads will be notified.
2)All will resume operations as soon as CalculatorThread notified and it exits the synchronized block, where “notifyAll()” resides.
Case V
ThreadA, ThreadB, ThreadC all wait on CalculatorThread to finish a certain task.
CalculatorThread has NO notify() or notifyAll() calls inside it’s run().
CalculatorThread is still alive (not dead) when ThreaA,ThreadB,ThreadC hit their wait() call.
1)No Reader thread will be notified.
2)All Reader threads will wait(). But now their wait() method behaves as the yield() method. ( Exactly as yield. It won’t release any locks it has now. But remember it has already released the lock on CalculatorThreard object, at the time it hit the wait() call)
3)So as soon as CalculatorThread exits it’s run() method, all Reader threads resume their operations.