Using "synchronized" but still getting IllegalMonitorStateException

L

laredotornado

Hi,

I'm trying to execute this bit of JUnit test code (Java 1.5):

public void testEventLoggerFailure() {
try {
synchronized(this) {
EventLogger el = new EventLogger(null,
null,
null,
null);
el.start();
el.wait();
} // synchronized
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
} // catch
} // testEventLoggerFailure

but yet, at the "el.wait()" line the below exceptioin is thrown ...

java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:474)
at ConnectionErrorsTest.testEventLoggerFailure
(ConnectionErrorsTest.java:113)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run
(JUnitTestRunner.java:297)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch
(JUnitTestRunner.java:672)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main
(JUnitTestRunner.java:567)

Any ideas? Thanks, - Dave
 
M

Mike Schilling

Peter Duniho wrote:
?
That said, if you're going to wait on a synchronization object, it's
not sufficient to have synchronized on any random object. You have
to own the monitor for the object you're trying to wait on.
Obviously in this case, "this" isn't the appropriate object required
to hold the monitor for, for calling "el.wait()".

Not to mention that el.wait looks like an American tourist trying to
speak Spanish.
 
R

Robert Klemme

I'm trying to execute this bit of JUnit test code (Java 1.5):

public void testEventLoggerFailure() {
try {
synchronized(this) {
EventLogger el = new EventLogger(null,
null,
null,
null);
el.start();
el.wait();
} // synchronized
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
} // catch
} // testEventLoggerFailure

but yet, at the "el.wait()" line the below exceptioin is thrown ...

java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method)
> ...
Any ideas? Thanks, - Dave

You can only wait on the monitor that you are holding, i.e. "this" in
the case above. Frankly, I have no idea what you are trying to achieve
with the code and it seems you do not know how synchronized, wait and
notify work. I suggest you get yourself a copy of Doug Lea's book:

http://www.informit.com/store/product.aspx?isbn=0201310090&rll=1

Cheers

robert
 
L

Lew

Try using gentler indentation on Usenet!

Robert said:
You can only wait on the monitor that you are holding, i.e. "this" in
the case above.  Frankly, I have no idea what you are trying to achieve
with the code and it seems you do not know how synchronized, wait and
notify work.  I suggest you get yourself a copy of Doug Lea's book:

http://www.informit.com/store/product.aspx?isbn=0201310090&rll=1

A great book.

You can also refer to the Javadoc for 'wait()':
<http://java.sun.com/javase/6/docs/api/java/lang/Object.html#wait()>
where it makes the same point.

The Javadocs are the first line of defense against an error in an API
call.

Well, they ought to be, at least.

It's also a good idea for every Java programmer to be cognizant of the
behavior of at least java.lang.Object's methods.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top