Why must wait() always be in synchronized block

后端 未结 10 1345
傲寒
傲寒 2020-11-22 06:18

We all know that in order to invoke Object.wait(), this call must be placed in synchronized block, otherwise an IllegalMonitorStateException is thrown. But what\'s t

10条回答
  •  佛祖请我去吃肉
    2020-11-22 07:11

    When you call notify() from an object t, java notifies a particular t.wait() method. But, how does java search and notify a particular wait method.

    java only looks into the synchronized block of code which was locked by object t. java cannot search the whole code to notify a particular t.wait().

提交回复
热议问题