if I have two locks nested
say (I am concerning java here)
synchronized (clientInfMutex) {
...
synchronized (clientInfMutex) {
//Will this part executable?
}
}
Will the inner part executable? the concern is that when the outer lock is getting the lock, the inner lock might not be able to lock the lock again. If the inner part is not executable, is there a mechanism, such that if I hold the lock in the outer lock already, I will be able to access the inner lock without a problem? Thanks.
Yes.
Because the thread will already have the lock.
In Java native locks are reentrant
Everytime the JVM hits a synchronized block, it checks if it has that lock, if no, then it waits for that lock, or else it increments an internal counter for that lock, and executes the enclosed block
Note that this is assuming that
clientInfMutexpoints to same object in both statements. If, for e.g., you did aclientInfMutex = new Object()between the two synchronized blocks, then your thread will wait for the lock of the new object.