• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IllegalMonitorStateException - Different in J2ME?

 
Alan Walker
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Does anyone know of a difference between J2ME and J2SE in the conditions that cause an IllegalMonitorStateException to be thrown?
When I forgot to synchronize a call to the notifyAll method in a midlet, it didn't throw an exception most of the time, even though it seems like it should have, because the current thread did not own the monitor of the object whose notifyAll method was being called.
Consider the following class (I have created this to demonstrate my question - my own original code is too full of irrelevant complications to be a good illustration of the issue).


If I create an instance of this class in a J2SE applet, the constructor throws an IllegalMonitorStateException. The exception can be eliminated by putting the notifyAll call inside a synchronized(this) {} block. All this seems to be just the way it should be.
But now, if I put the above code (without the notifyAll being synchronized) into a midlet and create an instance of the ThreadTest class, there is no exception. I have observed this on various emulators and on a Nokia series 40 phone (6610).
However, if I comment out the t.start() line, an IllegalMonitorStateException is thrown. It seems as if the existence of a waiting thread at the time of the call to notifyAll prevents the exception.
Has anybody come across this before? Am I misunderstanding something about this exception? I'd be grateful for any enlightenment.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic