The synchronization mechanism includes a concept - an object monitor. When wait () is called, the monitor is queried, and further execution is paused until a monitor is received or an InterruptedException occurs. When notification () is called, the monitor is released.
Take the script if wait () and notify () were placed in the Thread class instead of the Object class. At some point in the code, currentThread.wait() is called and then accesses the anObject .
When currentThread.wait () is called, the currentThread monitor is monitored, and no further execution is performed until a monitor is detected or an exception is thrown. Now, in the standby state, if the foo() method of another anotherObject in currentThread is called from another thread, it gets stuck even if the called foo() method does not have access to anObject . If the first wait () method was called on anObject , instead of the thread itself, other method calls (without accessing anObject ) on objects in the same thread were not stuck.
Thus, calls to the wait () and notify () methods for the Object class (or its subclasses) provide higher concurrency and why these methods are in the Object class and not in the Thread class.
Programmer in Paradise Nov 20 '09 at 12:06 2009-11-20 12:06
source share