Practice mode Java

What happens if a Java thread calls obj.wait() without owning the monitor of obj?

Correct answer D. An IllegalMonitorStateException is thrown

Explanation

The wait, notify and notifyAll methods require the calling thread to hold the object's monitor, normally by being inside a synchronized block on that object. Violating this cannot be detected by the compiler in general, so the JVM throws IllegalMonitorStateException at run time. Wait should also be called in a loop that rechecks its condition.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.