Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

weird Integer's moniter state.

Thread view: 
yk - 15 Nov 2007 07:21 GMT
HI there,I ran into a really weird problem today,I wrote the code when
I'm trying to get my Producer-Consumer-model running.

I got the running error exception like this:
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException
       at java.lang.Object.notify(Native Method)
       at Lock$Increaser.run(Lock.java:22)
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
       at java.lang.Object.notify(Native Method)
       at Lock$Decreaser.run(Lock.java:39)

Seems like is the Integer class that caused the weird problem,can
anyone tell me how this happens,maybe the autoboxing or something
else?

public class Lock{
    Integer i=new Integer("3");
    Increaser in=new Increaser();
    Decreaser de=new Decreaser();
    public static void main(String[] args){
        Lock l=new Lock();
        l.in.start();
        l.de.start();
    }
class Increaser extends Thread{
    public void run(){
        while(true){
            synchronized(i){
                if(i>10){
                    try{
                        i.wait();
                    }catch(InterruptedException e){
                    }
                    System.out.println("increaser waiting");
                }
                i++;
                i.notify();
            }
        }
    }
}
class Decreaser extends Thread{
    public void run(){
        while(true){
            synchronized(i){
                if(i<=0){
                    try{
                        i.wait();
                    }catch(InterruptedException e){
                    }
                    System.out.println("decreaser waiting");
                }
                i--;
                i.notify();
            }
        }
    }
}
}
Gordon Beaton - 15 Nov 2007 07:59 GMT
> HI there,I ran into a really weird problem today,I wrote the code when
> I'm trying to get my Producer-Consumer-model running.
[quoted text clipped - 6 lines]
>         at java.lang.Object.notify(Native Method)
>         at Lock$Decreaser.run(Lock.java:39)

> Seems like is the Integer class that caused the weird problem,can
> anyone tell me how this happens,maybe the autoboxing or something
> else?

I believe that when you do this:

 Integer i = new Integer(3);
 i++;

...that through autoboxing it's equivalent to:

 Integer i = new Integer(3);
 i = new Integer(i.intValue() + 1);

So you don't use the same object reference at the start and end of
your synchronized block, i.e. the object you synchronize on isn't the
one you notify.

/gordon

--
Mark Jeffcoat - 15 Nov 2007 08:56 GMT
>   Integer i = new Integer(3);
>   i = new Integer(i.intValue() + 1);
>
> So you don't use the same object reference at the start and end of
> your synchronized block, i.e. the object you synchronize on isn't the
> one you notify.

Exactly correct.

More precisely, this doesn't have anything to do with
autoboxing -- any class that you manipulate by having
your variable point to a new object on every operation
would work the same way.

E.g., with
 String s = " stuffs ";
 s = s.trim();

The 's' after the assignment in line 2 is not the
same Object as the first 's', so they have entirely
separate monitors.

Signature

Mark Jeffcoat
Austin, TX

yk - 15 Nov 2007 13:24 GMT
> I believe that when you do this:
>
[quoted text clipped - 9 lines]
> your synchronized block, i.e. the object you synchronize on isn't the
> one you notify.

Thanks for your reply,gordon.Not realizing the Integer is quite
similar to String in this kind of condition that make me so confused.
BTW:I really appreciate your example,Mark.
Roedy Green - 15 Nov 2007 23:15 GMT
>IllegalMonitorStateException

see
http://mindprod.com/jgloss/runerrormessages.html#ILLEGALMONITORSTATEEXCEPTION
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.