Junior — Middle
If a method is marked with the keyword synchronized, which part of the program will act as a monitor?
sobes.tech AI
Answer from AI
In Java (and Android), the keyword synchronized is used to synchronize access to critical sections of code. When a method is marked as synchronized, the monitor is the object on which the method is called:
- If the method is non-static — the monitor is the current object (
this). - If the method is static — the monitor is the class object (
Class).
Thus, the monitor is the object on which the lock is applied to ensure mutual exclusion when executing a synchronized method.