gpt4 book ai didi

java同步和异常处理

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:00:22 24 4
gpt4 key购买 nike

如果我有一个 synchronized block ,并且在该 block 内的某处抛出一个未在 synchronized block 内捕获的异常,当异常传播出它时是否会放弃锁?(同步块(synchronized block))

synchronized( mutex )
{
throw new Exception( "" );
}

最佳答案

锁总是被释放。

来自 JLS §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."

关于java同步和异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8375172/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com