Does the Object.wait () method restore the monitor when it throws an exception?

The Java documentation I use makes it clear that the Object.wait() method requests a linked monitor before returning it, whether it has been notified or is a false wakeup; any return to the normal method will precede the re-registration of the monitor.

However, it is a little less clear what will happen in the Object.wait() event throws an exception, for example, Interrupted Exception . I assume that it really reloads the lock before throwing an exception. However, the documentation is not very clear, so I'm not 100% sure ...

Here is the document I'm looking at: http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#wait%28%29

So, is my conclusion really correct, or should my call code address the state of the monitor (for example, if necessary, if necessary) after an exception is thrown?

+6
source share
1 answer

JLS indicates this in much more detail than Object#wait Javadoc. According to this text, the castle must be unconditionally restored. Quoting the corresponding bits:

  • The thread theme is added to the object set of expectations m and performs n unlocks on m.

  • Thread t does not follow any further instructions until it is removed from m wait set. A thread can be removed from the wait set due to any of the following actions and will resume after some time:

    • [...]

    • The interrupt action is performed on t.

  • Thread t performs n lock actions on m.

+5
source

Source: https://habr.com/ru/post/948974/


All Articles