Results 1 to 7 of 7
Thread: IllegalMonitorStateException
- 09-14-2012, 08:54 PM #1
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 200
- Rep Power
- 5
IllegalMonitorStateException
I'm working on an EventManager system for my latest project, and I want it to work in its own thread. Naturally, I want it to wait while there are no events to process, then wake up when there is work to be done. But I seem to be getting a java.lang.IllegalMonitorStateException when I implement it, so I'm guessing that I'm not quite doing it right. Help appreciated...
Java Code:package org.nubcraft.nd.event.handlers; import java.util.HashMap; import java.util.LinkedList; import java.util.Map; import org.nubcraft.api.threading.GracefulRunnable; import org.nubcraft.nd.core.ND; import org.nubcraft.nd.event.Event; public class EventManager extends GracefulRunnable { private final Map<Event.Type, EventHandler> handlers = new HashMap<Event.Type, EventHandler>(); private final LinkedList<Event> queue = new LinkedList<Event>(); public EventManager() { } @Override public void _run() { while(queue.isEmpty()) { try { queue.wait(); } catch(InterruptedException e) { ND.logger.warn(e, true); } } while(!queue.isEmpty()) { Event event; synchronized(queue) { event = queue.remove(0); } try { handlers.get(event.getType()).handle(event); } catch(Exception e) { ND.logger.warn(e, true); } } } public void queue(Event event) { synchronized(queue) { queue.add(event); } queue.notify(); } @Override public void onStop() { ND.logger.info(queue.size() + " events unhandled on shutdown", true); } }Java Code:package org.nubcraft.api.threading; public abstract class GracefulRunnable implements Runnable, _Runnable, Stoppable { private boolean running; public GracefulRunnable() { } public Thread start() { Thread thread = new Thread(this); thread.start(); return thread; } public Thread start(String threadName) { Thread thread = new Thread(this, threadName); thread.start(); return thread; } @Override public void requestStop() { running = false; } @Override public final void run() { running = true; while(running) { _run(); } onStop(); } }
-
Re: IllegalMonitorStateException
Could you please tell us if the IllegalMonitorStateException text indicates which line is throwing the exception, if it always occurs, if it's always at the same location, and any other information that might help us?
- 09-14-2012, 10:10 PM #3
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 200
- Rep Power
- 5
Re: IllegalMonitorStateException
Exception in thread "Event Manager" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:503)
at org.nubcraft.nd.event.handlers.EventManager._run(E ventManager.java:28)
at org.nubcraft.api.threading.GracefulRunnable.run(Gr acefulRunnable.java:40)
at java.lang.Thread.run(Thread.java:722)
Java Code:... while(queue.isEmpty()) { try { queue.wait(); // <-- here } catch(InterruptedException e) { ND.logger.warn(e, true); } } ...Last edited by AndrewM16921; 09-14-2012 at 10:16 PM.
-
Re: IllegalMonitorStateException
I believe that you need to be in a synchronized block for Object#wait() to work correctly. It looks like your wait method is called in a non-synchronized section of code.
- 09-14-2012, 11:03 PM #5
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 200
- Rep Power
- 5
Re: IllegalMonitorStateException
Oh wow. That fixed it. How simple, lol. Thanks! :)
-
Re: IllegalMonitorStateException
You're welcome! I've not used them, but as of Java 1.5 I believe that there are other tools available for locking, waiting and notifying threads, tools that allow greater flexibility and that are part of the java.util.concurrent library including Locks. You might want to look into them a bit.
- 09-14-2012, 11:46 PM #7
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 200
- Rep Power
- 5
Similar Threads
-
IllegalMonitorStateException from synchronized block
By svdeepha in forum Threads and SynchronizationReplies: 1Last Post: 04-26-2010, 07:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks