當從finally
塊內部解鎖時,我的程序會拋出IllegalMonitorStateException
。 Javadoc說,如果當前線程沒有保存這個鎖,就拋出這個異常。但是,如果程序到達finally塊,它必須先獲得一個鎖。鎖在其他地方沒有解鎖。鎖可能「迷路」?ReentrantReadWriteLock丟失 - 引發IllegalMonitorStateException
代碼示例:
final ReadWriteLock rwLock = new ReentrantReadWriteLock();
public void doSomething() {
Lock lock = rwLock.writeLock();
try {
doStuff();
} finally {
lock.unlock();
}
}