Why wait should always be in synchronized block

Posted by diy on Stack Overflow See other posts from Stack Overflow or by diy
Published on 2010-05-06T08:08:58Z Indexed on 2010/05/06 8:28 UTC
Read the original article Hit count: 199

Filed under:
|
|
|

Hi gents,

We all know that in order to invoke Object.wait() , this call must be placed in synchronized block,otherwise,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() ?

What is the potential damage if it was possible to invoke wait() outside synch block, retaining it's semantics - suspending the caller thread ?

Thanks in advance

© Stack Overflow or respective owner

Related posts about java

Related posts about concurrency