why wait/notify/notifyAll methods are not synchronized in java ?

后端 未结 7 1539
生来不讨喜
生来不讨喜 2020-12-05 10:20

in Java whenever we need to call wait/notify/notifyAll, we need to have access to object monitor (either through synchronized method or through synchronized block). So my q

7条回答
  •  感动是毒
    2020-12-05 10:52

    Someone more experienced with multithreading should feel free to step in, but I believe this would remove the versatility of synchonized blocks. The point of using them is to synchronize on a particular object which functions as the monitored resource/semaphore. wait/notify methods are then used to control execution flow within the synchronized block.

    Note that synchronized methods are shorthand for synchronizing on this for the duration of the method (or the class for static methods). Synchronizing wait/notify methods themselves would remove the point of their use as stop/go signals between threads.

提交回复
热议问题