In order to properly understand the issues and solutions for concurrency in Java, I was going through the official Java tutorial. In one of the pages they defined Intrin
Seems you have one misunderstanding (dunno if it caused the wrong conclusion) that no one has pointed out. Anyway, a brief answer:
Intrinsic Lock: Just think it as, every object in JVM has internally a lock. synchronized
keywords tries to acquire the lock of the target object. Whenever you synchronized (a) { doSomething; }
, what actually happens is
a
is acquireddoSomething
)a
and I wish you know
public synchronized void foo() {
doSomething;
}
is conceptually the same as
public void foo() {
synchronized(this) {
doSomething;
}
}
Ok, go back to your question, the biggest problem, imho, is :
For me this means that once I call a synchronized method from one of the threads, I will have hold of the intrinsic lock of the thread and since...
It is wrong. When you call a synchronized method, you are not get hold of the lock of the thread.
Instead, that thread will own the intrinsic lock of the object that is "owning" the method.
e.g. in thread1, you called a.foo()
, and assume foo()
is synchronized. thread1 is going to acquire the intrinsic lock of the object a
referring.
Similarly, if AClass.bar()
is called (and bar
is synchronized and a static method), the intrinsic lock of AClass
Class object will be acquired.