Side effects of throwing an exception inside a synchronized clause?

前端 未结 2 595
無奈伤痛
無奈伤痛 2020-12-01 11:41

Are there any unclear side effects to throwing an exception from within a synchronized clause? What happens to the lock?

private void doSomething() throws E         


        
2条回答
  •  一整个雨季
    2020-12-01 12:02

    As you would hope, the lock is released normally.

    For reference, the appropriate section of the JLS which guarantees this behaviour is § 14.19:

    If execution of the Block completes normally, then the lock is unlocked and the synchronized statement completes normally. If execution of the Block completes abruptly for any reason, then the lock is unlocked and the synchronized statement then completes abruptly for the same reason.

    ('abrupt completion' is defined elsewhere in the JLS to include exceptions from JVM, exceptions raised by throw, and use of the break, continue, or return statements to transfer outside the block.)

提交回复
热议问题