Each object has a built-in monitor, expecting it to be used by some kind of code. In fact, most objects are never used as a monitor, so monitors should not be created until they are used. Instead of implementing this function, since each object has its own monitor monitor field, think of it as an implementation as a JVM with global HashMap monitors.
A possible implementation is this: whenever a synchronized block is entered, the JVM looks at the synchronized object on the map (monitors). If he finds it, he will get a monitor. If he does not find it, he enters the critical section on the map. Then it again searches for the object, because another thread can create it between the previous scan and entering the critical section. If it does not already exist, it creates a monitor for the synchronized object and leaves a critical section
kandarp Mar 24 '12 at 3:30 2012-03-24 03:30
source share