Skip to content

Tag: wait

Java Delay/Wait

How do I delay a while loop to 1 second intervals without slowing down the entire code / computer it’s running on to the one second delay (just the one little loop). Answer

Why must wait() always be in synchronized block

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 the reason for making this restriction? I know that wait() releases the monitor, but why do we need to explicitly acquire the monitor by making particular block synchronized and then release the monitor by calling wait()?