Results 1 to 7 of 7
  1. #1
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default 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();
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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?

  3. #3
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default 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 11:16 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  5. #5
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default Re: IllegalMonitorStateException

    Oh wow. That fixed it. How simple, lol. Thanks! :)

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  7. #7
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default Re: IllegalMonitorStateException

    Ok I shall, thanks again.

Similar Threads

  1. IllegalMonitorStateException from synchronized block
    By svdeepha in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-26-2010, 08:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •