When we talk about intrinsic lock we refer to the object for which we ask the lock or for the synchronized method?
The lock is on the object or on it's sync method?
I'm confused!
When we talk about intrinsic lock we refer to the object for which we ask the lock or for the synchronized method?
The lock is on the object or on it's sync method?
I'm confused!
Synchronized methods locks the method on the object
synchronized void methodA () {
....
}
is somehow equivalent to
void methodA () {
synchronized (this) {
....
}
}
Intrinsic locks are on the object:
class A
{
public synchronized void method1(){...}
public synchronized void method2(){...}
}
If thread A is in method1 then threadB cannot enter method2.
The lock is part of the Object. Every Object has one and it may be locked in two ways:
synchronized
modifier on an instance method of the Class to lock the associated objectsynchronized(object) {}
blockSimilarly, you can lock the Class of an Object instead of the Object itself (bares mentioning separately to understand the synchronized
modifier with a static
method):
synchronized
modifier on a static method of the Class to lock the Classsynchronized(clazz) {}
block where class
is the Class of the Object