TechTalkz.com Logo Ask the Experts!

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > Java

weird Integer's moniter state.

Java

 
 
Thread Tools Display Modes
Unread 15-11-2007, 07:58 AM   #1
yk
Guest
 
Posts: n/a
weird Integer's moniter state.

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();
}
}
}
}
}
 
Unread 15-11-2007, 09:00 AM   #2
Gordon Beaton
Guest
 
Posts: n/a
Re: weird Integer's moniter state.

On Wed, 14 Nov 2007 23:21:03 -0800 (PST), yk wrote:
> 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?


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

--
 
Unread 15-11-2007, 10:00 AM   #3
Mark Jeffcoat
Guest
 
Posts: n/a
Re: weird Integer's moniter state.

Gordon Beaton <n.o.t@for.email> writes:

> 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.

--
Mark Jeffcoat
Austin, TX
 
Unread 15-11-2007, 02:03 PM   #4
yk
Guest
 
Posts: n/a
Re: weird Integer's moniter state.

> 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.
>

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.
 
Unread 16-11-2007, 12:12 AM   #5
Roedy Green
Guest
 
Posts: n/a
Re: weird Integer's moniter state.

On Wed, 14 Nov 2007 23:21:03 -0800 (PST), yk <ykrocku***********>
wrote, quoted or indirectly quoted someone who said :

>IllegalMonitorStateException


see
http://mindprod.com/jgloss/runerrorm...STATEEXCEPTION
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
 

Thread Tools
Display Modes



< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT. The time now is 12:50 PM.


vBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2013, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional