Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Simple(?) synchronized() block question

Reply
Thread Tools

Simple(?) synchronized() block question

 
 
noident@my-deja.com
Guest
Posts: n/a
 
      06-30-2006
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
The following simple program fails, and I can't figure out what's
wrong. I must be missing something simple.

import java.util.*;

public class Foo extends Thread
{
private String s;
public Foo(String ss) { s = ss; }

public void run()
{
synchronized(s) // changing this line to 'synchronized(this)' fixes
the problem
{
try
{
wait();
}
catch(InterruptedException e)
{
System.err.println(e);
System.exit(1);
}
}
}

public static void main(String[] args)
{
new Foo("abc").start();
}
}

Running the above produces this:
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:474)
at Foo.run(Foo.java:14)

my java version is:
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode)

Any suggestions will be very much appreciated.

 
Reply With Quote
 
 
 
 
chins_inus@yahoo.com
Guest
Posts: n/a
 
      06-30-2006
Hi,
So basically u r getting this error coz u r calling wait method
implicitly on 'this' and synchronizing String s. so if u write s.wait()
it solves ur problem.

I hope it helps...


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
> The following simple program fails, and I can't figure out what's
> wrong. I must be missing something simple.
>
> import java.util.*;
>
> public class Foo extends Thread
> {
> private String s;
> public Foo(String ss) { s = ss; }
>
> public void run()
> {
> synchronized(s) // changing this line to 'synchronized(this)' fixes
> the problem
> {
> try
> {
> wait();
> }
> catch(InterruptedException e)
> {
> System.err.println(e);
> System.exit(1);
> }
> }
> }
>
> public static void main(String[] args)
> {
> new Foo("abc").start();
> }
> }
>
> Running the above produces this:
> Exception in thread "Thread-0" java.lang.IllegalMonitorStateException
> at java.lang.Object.wait(Native Method)
> at java.lang.Object.wait(Object.java:474)
> at Foo.run(Foo.java:14)
>
> my java version is:
> java version "1.5.0_06"
> Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
> Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode)
>
> Any suggestions will be very much appreciated.


 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      06-30-2006
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
 
Reply With Quote
 
noident@my-deja.com
Guest
Posts: n/a
 
      06-30-2006
Thank you very much to those who replied.
Of course! wait() is the Object's method, not the Thread's. My java
textbook mentioned that but didn't stress it well enough so I missed
it.
Thanks again.

 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      06-30-2006
wrote:
> Thank you very much to those who replied.
> Of course! wait() is the Object's method, not the Thread's. My java
> textbook mentioned that but didn't stress it well enough so I missed
> it.
> Thanks again.
>


Note that even if the wait were somehow resolved for the thread rather
than "this", it could not tell which object to wait on. The thread can
be synchronized simultaneously on several different objects.

Patricia
 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      06-30-2006
wrote:
> public class Foo extends Thread


Extending Thread is almost always a bad idea. If your book tells you to
do that, burn it.

> {
> private String s;
> public Foo(String ss) { s = ss; }
>
> public void run()
> {
> synchronized(s) // changing this line to 'synchronized(this)' fixes
> the problem


Using a client supplied String for a lock isn't a great idea. The String
may be shared with the rest of the code in the JRE.

A handy little trick is to declare a nested or local class called Lock
with an empty body. The full name of classes are shown if you use
ctrl-\, ctrl-break, jstack or similar. A custom class name will help you
identify locks.

> {
> try
> {
> wait();


Even if the Thread was the right object to wait on, Thread is a bad
choice of lock. In Sun JVMs, Thread uses itself as a lock for internal
operations. Attempting to lock it yourself may give strange results.

You need to be careful what you synchronise on. An easy mistake is to
lock on an outer class instance, and then within in an inner class lock
on the inner class instance because this is different.

> }
> catch(InterruptedException e)
> {
> System.err.println(e);
> System.exit(1);


Seems a little extreme...

> }
> }
> }
>
> public static void main(String[] args)
> {
> new Foo("abc").start();
> }
> }


Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Fo:Block can you check to see if a block contains any text by using the block id? morrell XML 1 10-10-2006 07:18 PM
Problem with enterprise application block - data block Showjumper ASP .Net 1 03-19-2005 03:48 PM
Block DIV within a block DIV? Noozer HTML 3 01-06-2005 10:24 PM
XML schema validation of one xml block based on values from another xml block Andy XML 0 11-18-2004 11:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57