One of hundreds of answers available with our premium content service.
A: A synchronized method uses what is called an intrinsic lock to control thread execution, an object reference that is used as a monitor for the lock. Synchronized instance methods implicitly use their own object instance as the monitor. Only one thread can hold the monitor object at a given moment and execute the synchronized code.
Static synchronized methods may be executed when there is no instance of the class to serve as a lock monitor, so must use a different lock object. When class methods are invoked the Java runtime system automatically creates an object of the type Class as the context for the method to be executed. If the class method happens to be synchronized, this Class instance is the intrinsic lock used as the monitor object for synchronization.
Since synchronized instance and class methods have different monitor objects, there is no effective synchronization control between them and no guarantee to their sequence of execution. If a closer level of control is required, it may be necessary to remove the synchronized modifier from the instance method and create a synchronized block inside the method that uses the Class instance as its monitor, as in the example below.
… full answer hidden
Premium members click below for full answer
Which has priority with a static synchronized thread?
Your premium content subscription is a single payment that gives you: