I know how synchronized methods in java work.
Suppose we have 3 synchronized methods in a class. If a Thread accesses 1 of the available methods, the other 2 get blocked for other threads.
If we change the synchronization implementation in 1 of methods to :
synchronized (this) {...}
Does it still work the same way and block the 2 other methods?
Yes it does.
uses the object’s instrinsic lock (
this), so any other synchronized block using that lock:would block if the lock is already held.
Note: if a method is static, the picture is different, because the lock is not
thisany more, it isYourObject.class. In the example below, nothing prevents a thread T1 to runmethodwhile another thread T2 runssomeOtherMethod, because the 2 sections don’t use the same lock: