Results 1 to 10 of 10
- 05-24-2011, 08:44 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 93
- Rep Power
- 0
Can anyone explain why this gives an IllegalMonitorException + how to fix it?
This is an extract from a GUI Tic Tac Toe game.
Thanks.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(); } }
- 05-24-2011, 08:45 PM #2
Member
- Join Date
- Jan 2011
- Posts
- 93
- Rep Power
- 0
N.B. I wanted the winning cells to be highlighted one by one.
- 05-25-2011, 01:20 AM #3
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!
- 05-25-2011, 01:23 AM #4
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!
- 05-25-2011, 07:36 AM #5
Member
- Join Date
- Jan 2011
- Posts
- 93
- Rep Power
- 0
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!
- 05-25-2011, 08:37 AM #6
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!
- 05-25-2011, 09:30 AM #7
Member
- Join Date
- Jan 2011
- Posts
- 93
- Rep Power
- 0
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.
- 05-25-2011, 10:15 AM #8
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!
- 05-25-2011, 11:08 AM #9
Member
- Join Date
- Jan 2011
- Posts
- 93
- Rep Power
- 0
- 05-25-2011, 11:39 AM #10
Similar Threads
-
Anyone can explain this?
By kazumahits in forum New To JavaReplies: 1Last Post: 03-08-2011, 02:03 AM -
Please Explain me how i got this output.. I am new to java .. so please Explain me...
By vicky82 in forum New To JavaReplies: 2Last Post: 12-13-2010, 01:34 PM -
Please Explain me how i got this output.. I am new to java .. so please Explain me...
By vicky82 in forum New To JavaReplies: 3Last Post: 12-13-2010, 07:22 AM -
Can somebody explain me this plz
By ccie007 in forum New To JavaReplies: 4Last Post: 05-20-2010, 07:47 PM -
Can someone explain why...
By Krooger in forum AWT / SwingReplies: 1Last Post: 11-19-2009, 06:59 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks