Why must wait() always be in synchronized block

后端 未结 10 1134
傲寒
傲寒 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:14

    directly from this java oracle tutorial:

    When a thread invokes d.wait, it must own the intrinsic lock for d — otherwise an error is thrown. Invoking wait inside a synchronized method is a simple way to acquire the intrinsic lock.

提交回复
热议问题