https://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html Says "In this example, the addName method needs to synchronize changes to lastName and nameCount, but also needs to avoid synchronizing invocations of other objects' methods. (Invoking other objects' methods from synchronized code can create problems that are described in the section on Liveness.)"
I actually want to synchronize another object. There are 3 classes, AdminImpl, UserImpl, ServerState.
The same instance of ServerState is an attribute in both AdminImpl and UserImpl.
If I write a critical section in UserImpl with synchronized(this)
, will it acquire the lock of both UserImpl and the instance of ServerState that is an attribute of UserImpl? Meaning, will AdminImpl not be able to be in a (different) critical section at the same time as UserImpl, because to be in a synchronized section of AdminImpl it needs to acquire both locks (of AdminImpl and ServerState), and can't acquire the second one because it is acquired by UserImpl?
I have found some questions about synchronized such as java method synchronization object Confusing Java synchronized method, synchronized(this), and synchronized class What's the difference between synchronized(this) and synchronized(some other object)
but none touches on this point