This is probably a very silly question. I am reading through the documentation of interfaces and classes in packages java.util.concurrent.atomic
and java.util.concurrent.locks
. I find everywhere that
void lock() – acquire the lock if it’s available; if the lock isn’t available a thread gets blocked until the lock is released
What I am not sure about is, what resource is exactly getting locked? The code snippet examples show
Lock lock = new ReentrantLock();
lock.lock();
try {
// access to the shared resource
} finally {
lock.unlock();
}
Does whatever that is used under the call of lock()
get locked? How does JVM know that before getting to that line? And what if there are multiple resources between lock()
and unlock()
? I guess I am having this question because I am reading this right after reading synchronization
and it has very specific way of saying what to lock - like: synchronized(resourceReferenceThatNeedsToBeLocked)
I reseached a lot and yet can't find answer for this question.