What will happen if thread throws a Exception inside synchronised block

后端 未结 3 1979
北恋
北恋 2021-02-05 18:18

Consider multiple threads are trying to access critical section, what will happen one thread that occurs Exception inside a synchronized block it having wait() and notify() to

3条回答
  •  无人及你
    2021-02-05 19:12

    The synchronization monitor will be released: "If execution of the body is ever completed, either normally or abruptly, an unlock action is automatically performed on that same monitor." Java Language Specification 17.1. Synchronization.

    Other threads will be able to continue synchronizing, and calling wait and notify.

    If the thread with the exception is holding some critical program logic resource, you may need to use try-finally to ensure it is released.

提交回复
热议问题