Dmi*_*tov 2 java synchronization locking
离开范围时,释放资源的最佳方法是什么(在这种情况下解锁ReadWriteLock)?如何涵盖所有可能的方式(退货,休息,例外等)?
Mic*_*ker 12
try/finally块是最接近这种行为的东西:
Lock l = new Lock();
l.lock(); // Call the lock before calling try.
try {
// Do some processing.
// All code must go in here including break, return etc.
return something;
} finally {
l.unlock();
}
Run Code Online (Sandbox Code Playgroud)