Suppose threads aThread
and bThread
are both accessing a shared object named sharedOb
.
aThread
has just executed:
sharedOb.wait();
What code can bThread
execute in order to get aThread
out of the waiting state, no matter what other conditions prevail?
A. aThread.notify(); B. aThread.notifyAll(); C. aThread.interrupt(); D. sharedOb.notify(); E. sharedOb.notifyAll();
C, E.
The notify()
and notifyAll()
methods affect the threads that are waiting for the object on which the call is made.
In other words, you notify the object, not the thread.
So A and B don't work.
When a thread is interrupted, it leaves the wait state and enters the InterruptedException handler, so C is correct.
D does not work because there might be multiple threads waiting for sharedOb
.
E works because it guarantees that all waiting objects are moved out of the waiting state.