Re: Simple(?) synchronized() block question



noident@xxxxxxxxxxx wrote:
Greetings!
I am trying to make a simple thread/conncurrency example work.
What I am trying to do in the following simple code example is:
1. Start a thread
2. Make the started thread become another object's (String s) monitor
by executing a synchronized() statement
3. Call wait().
But it fails with an IllegalMonitorStateException.
If I understand the documentation correctly, I am under impression that
if the code within the synchronized() {} block is executing, the thread
is already the monitor of the object it's synchronizing on, but that
seems not to be the case :(
....
synchronized(s) // changing this line to 'synchronized(this)' fixes
the problem
{
try
{
wait();
}



In order to wait, you have to be in code synchronized on the object
whose wait method you are calling. You called, in effect, this.wait()
while synchronized on the string referenced by s. You must either
synchronize on this or call s.wait().

Patricia
.