Can a Java thread capture a lock while holding it?

If a thread contains a lock, what happens when a thread needs to enter another critical partition controlled by the same lock?

+4
source share
4 answers

Internal locks (synchronized) in Java are reentrant, so the JVM recognizes that the current thread is already holding this lock, and it will continue.

There are also explicit locks that are reentrant.

If locking is not reentrant, you can, for example, not use recursive methods.

+7
source

nothing: the system can determine which thread contains the lock to avoid blocking the thread.

+1
source

If a thread contains a lock, then it can enter any methods (synchronized or unsynchronized), but if some other thread wants to call a method, then the methods must be unsynchronized or it needs to wait to get a lock if it tries to call a synchronized method.

0
source

retentate blocking

for example a monitor , this will allow, because they have a sense of owenership

resource blocking

such as a semaphore , have no sense of ownership, therefore will lead to a deadlock if you do not have enough resources

0
source

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


All Articles