"Acquiring the lock associated with an object does not prevent other threads from accessing that object--the only thing that acquiring a lock prevents any other thread from doing is acquiring that same lock."
Your three methods are declared as being synchronized
. When one of those methods is invoked, like
MyClass instance = new MyClass();
instance.methodA();
the currently executing thread (call it A) will acquire that instance's monitor. No other thread will be able to acquire the same object's monitor until A has released it. Nothing prevents other threads from calling methods on that same object.
Say you had thread A doing
instance.methodA();
and thread B doing
instance.methodB();
Then, yes, thread B would have to wait for methodA
to complete (ie. for thread A to release the lock on the object referenced by instance
).
However, if thread A was doing
instance.methodA();
and thread B was doing
instance.toString();
thread B would not have to wait at all since toString()
does nothing with the object's monitor.