Junior — Middle
For which objects in programming is the synchronization mechanism via monitor available?
sobes.tech AI
Answer from AI
In Java, the synchronization mechanism through monitors is available for all objects, since each object has an intrinsic monitor (monitor lock). This means you can use the synchronized keyword on any object to ensure mutual exclusion when accessing its critical sections.
For example, you can synchronize a block of code on any object:
synchronized (someObject) {
// critical section
}
You can also declare methods as synchronized, which is equivalent to synchronizing on the monitor of the current object (this) for non-static methods or on the class object for static methods.