Can you tell me if following invocations are reentrant or not?
public class Foo {
public synchronized void doSomething() {}
public synchronized void doAnot
First of all, regarding reentrant locks in Java:
Synchronized blocks in Java are reentrant. This means, that if a Java thread enters a synchronized block of code, and thereby take the lock on the monitor object the block is synchronized on, the thread can enter other Java code blocks synchronized on the same monitor object.
Taken from here.
The two consecutive calls you described (in doToo
) will not be interfered unless another object has a reference to Too
's private Foo
, since, to access foo
, one needs to lock Too
. However, the calls do not invoke reentry as the locks are acquired and released for every call. They would be reentrant if doSomething
called doAnotherSomething
or vice versa.