Results 1 to 10 of 10
  1. #1
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default Can anyone explain why this gives an IllegalMonitorException + how to fix it?

    This is an extract from a GUI Tic Tac Toe game.

    Java Code:
    synchronized private void highlightWinningCells(int firstCell, int secondCell, int thirdCell )
    	{
    		Color winningCellHighlightColour = new Color(204,255,204);
    			
    			try{
    				cell[firstCell].setBackground(winningCellHighlightColour); 
    				Thread.currentThread().wait();
    				cell[secondCell].setBackground(winningCellHighlightColour); 
    				Thread.currentThread().wait();
    				cell[thirdCell].setBackground(winningCellHighlightColour); 
    				Thread.currentThread().wait();
    			}catch(Exception e)
    			{
    				e.printStackTrace();
    			}	
    	}
    Thanks.

  2. #2
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    N.B. I wanted the winning cells to be highlighted one by one.

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    You are calling wait() on the current Thread object, and you don't hold the monitor on it.

    You hold the monitor on "this" because you're in a synchronized method. So you just want to call wait(). (Which is, of course, shorthand for this.wait().)
    Get in the habit of using standard Java naming conventions!

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    Or perhaps I should say, that's why you're getting that error, and that's how to fix it. I'm not sure that waiting on "this" is what you actually want. Do you have another thread calling notify() on the same object to wake this thread up? Just a wild guess, but you probably want sleep(long), not wait().
    Get in the habit of using standard Java naming conventions!

  5. #5
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    sleep() is what i wanted, and that's what I used before - but that gave me an exception too (I can't remember which one it was. I'm at work now otherwise i'd check).
    It would highlight the first cell and then give me an exception.

    For the sake of my general knowledge, what is meant b monitor in this context? Thanks!

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    Yeah, sleep() can throw an InterruptedException. It's a checked exception so you have to handle it, but it won't be thrown unless some other thread calls interrupt() on the sleeping thread. (Or has called interrupt() on it before it attempts to sleep.)

    The monitor is the lock associated with every object.
    Get in the habit of using standard Java naming conventions!

  7. #7
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    I just remembered what the problem was with sleep(); - once the winner clicked on the winning cell, there would be a loooong pause and all the cells would highlight at once.

    That's not the effect I wanted. I'm not sure why it happens so I don't know how to fix it.

  8. #8
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    Aha. Kind of guessing here, but I bet you called highlightWinningCells in the Swing event handling thread (like, in or from an event listener). So it changed the variables representing the cells' background colors, but you wouldn't see it until it returned from that method and had a chance to redraw the GUI.

    Any method called by an event listener needs to return quickly, or the GUI will seem laggy.

    Unfortunately, I can't be much more help because I don't know my way around the Swing API very well. All I can tell you is that instead of sleeping in the Swing thread, you need to set up some kind of timer to change the cell colors at future intervals. Hopefully someone who knows more about Swing can chime in and help you with that.
    Get in the habit of using standard Java naming conventions!

  9. #9
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    Thanks a lot! I get what I did wrong.

    One thing though, I didn't understand this one bit of your explanation:

    Quote Originally Posted by kjkrum View Post
    So it changed the variables representing the cells' background colors, but you wouldn't see it until it returned from that method and had a chance to redraw the GUI.
    I'm not sure what you mean here?

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

Similar Threads

  1. Anyone can explain this?
    By kazumahits in forum New To Java
    Replies: 1
    Last Post: 03-08-2011, 02:03 AM
  2. Replies: 2
    Last Post: 12-13-2010, 01:34 PM
  3. Replies: 3
    Last Post: 12-13-2010, 07:22 AM
  4. Can somebody explain me this plz
    By ccie007 in forum New To Java
    Replies: 4
    Last Post: 05-20-2010, 07:47 PM
  5. Can someone explain why...
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-19-2009, 06:59 AM

Posting Permissions

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