Re: Simple(?) synchronized() block question
- From: Patricia Shanahan <pats@xxxxxxx>
- Date: Fri, 30 Jun 2006 02:03:38 GMT
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
.
- References:
- Simple(?) synchronized() block question
- From: noident
- Simple(?) synchronized() block question
- Prev by Date: Re: Simple(?) synchronized() block question
- Next by Date: Re: Simple(?) synchronized() block question
- Previous by thread: Re: Simple(?) synchronized() block question
- Next by thread: Re: Simple(?) synchronized() block question
- Index(es):