gpt4 book ai didi

java - 当我读Core.Java.Volume.I时,我无法理解这句话的含义

转载 作者:太空宇宙 更新时间:2023-11-04 12:06:27 26 4
gpt4 key购买 nike

当我阅读Core.Java.Volume.I时,我无法理解这句话的含义:

"Be careful to ensure that the code in a critical section is not bypassed by throwing an exception. If an exception is thrown before the end of the section,the finally clause will relinquish the lock, but the object may be in a damaged state."

我怎样才能确保这一点?抛出异常时不是必须绕过临界区的代码吗?

最佳答案

考虑这个伪代码:

Patient patient = service.getPatient(name);
hospitalEquipment.lock();
try {
patient.pauseLifeSupport();
// some code causing an exception
patient.resumeLifeSupport();
} finally {
hospitalEquipment.unlock();
}

在此伪代码中,异常导致“关键部分”被绕过,即恢复生命支持。您可以通过将调用移至 finally block 内的 resumeLifeSupport() 或采取一些紧急行为来解决此问题。

关于java - 当我读Core.Java.Volume.I时,我无法理解这句话的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40312480/

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