The possibility of an exception after locking the lock, but before the attempt - finally

I am wondering if a code like

lock.lock();
try {
    count++;
} finally {
    lock.unlock();
}

Is there a chance that the execution of the thread may be somehow completed after the method is executed lock, but before entering the block try-finally? This will result in a lock that will be made, but will not be released. Is there any line in the Java / JVM specification that gives us confidence that if the code is written using this idiom, there is no way to leave a locked lock?

My question is inspired by the answer to a question related to C # Can Monitor.Enter be excluded? which refers to two messages on MSDN

Monitor.Enter(...)
try
{
    ...
}
finally
{
    Monitor.Exit(..)
}

# , try-finally , JIT.

, /nitpicking, .

+4
1

- Java/JVM, , ?

, , java . - , () (). , . java monitorenter (https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-6.html#jvms-6.5.monitorenter) - . , monitorenter , , jvm. synchhonized monitorenter monitorexit - , jvm , jvm . , smth , jvm. , , native jvm, jvm.

. , , . , . - , , . - , - Java. JVM . java - (https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/Lock.html), OOB, , Rentrant RW .. . , , , , . , RLock , ( ). , , getan Error (https://docs.oracle.com/javase/7/docs/api/java/lang/Error.html). JVM, . - RLock Java OOB AbstractQueuedSynchronizer (https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/AbstractQueuedSynchronizer.html). , , , JVM .

. - , - , , . , , . , . , , . , , smb m. .

:

, - , try-finally?

, , . , , , . , . . 2+ . - . , , .

+3

Source: https://habr.com/ru/post/1687170/


All Articles