In the below If exception happens at syncMethod2() then how can i unlock the lock object?
public class ReEntrantLock {
ReentrantLock lock = new ReentrantLock();
void syncMethod1() {
lock.lock();
syncMethod2(); // throw new NullPointerException();
lock.unlock();
}
}
You use
try–finally:That pattern is also described in the
ReentrantLockJavaDoc.