Machine-translated by Google. Questions are authored in English.
Top

Discussion

Consider why wait() belongs inside a loop. Which statement is correct?

  • A.wait() should never be called in a loop.
  • B.wait() should be called in a loop that re-tests the condition.
  • C.wait() may be called with a plain if test.
  • D.wait() should be called in a loop only when more than one thread waits on the object.

Answer: B

The javadoc states a thread can wake without being notified, interrupted or timing out, and that applications must guard against it by testing the condition and continuing to wait if it does not hold. The documented idiom is while (condition does not hold) obj.wait(); - a while, not an if.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.