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

Discussion

Consider the monitor a thread must own to call wait(). Which statement is correct?

  • A.wait() may be called from any thread, but only notify() requires owning the monitor.
  • B.wait() must be called by a thread owning the object's monitor, or IllegalMonitorStateException is thrown.
  • C.wait() may be called from any thread.
  • D.wait() must be called from outside a synchronized block, or IllegalMonitorStateException is thrown.

Answer: B

All of wait, notify and notifyAll should only be called by a thread that owns the object's monitor, and calling one without that ownership throws IllegalMonitorStateException. In practice that means the call sits inside a synchronized block or method on the same object. The requirement applies equally to notify.

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

Post your comment

Please log in or register to post a comment.