For Programmers: Free Programming Magazines  


Home > Archive > Java Help > February 2005 > IllegalMonitorStateException :S









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author IllegalMonitorStateException :S
Andrew Bullock

2005-02-24, 4:01 pm

Hi

Im trying to implement some read/write locks in Java. (Thanks Tilman
Bohn for your earlier reply)


Ive got some working using a single MutEx Monitor, however my
implementatios is Write preferrencial.

So i re-wrote the code using two MutEx Monitors, one for reading and one
for writing to make it as unbaised as possible, and my code should work,
theres just one problem.

When i try to notify the read mutex object from a write thread (or vice
versa) i get the following error:

IllegalMonitorStateException

Which i kinda understand why, but what can i do about it?


Many thanks in advance

Andrew Bullock
Tilman Bohn

2005-02-24, 8:59 pm

In message <k5pTd.909$4O3.195@newsfe4-gui.ntli.net>,
Andrew Bullock wrote on Thu, 24 Feb 2005 18:32:48 GMT:

[...]
> When i try to notify the read mutex object from a write thread (or vice
> versa) i get the following error:
>
> IllegalMonitorStateException
>
> Which i kinda understand why, but what can i do about it?


You have to obtain the lock before calling notify(). That is, call
notify() within a synchronized (m){} block, where m is the monitor
whose wait()ers you want to notify(). The same goes for wait(). Also
make sure to only call the latter from a while(!condition()) loop,
as explained in at least two very recent threads in this group.

--
Cheers, Tilman

`Boy, life takes a long time to live...' -- Steven Wright
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com