We help IT Professionals succeed at work.

GUI Freezing when wait()'ing for an actionEvent

Savag3
Savag3 asked
on
Medium Priority
294 Views
Last Modified: 2012-05-05
Can someone explain why the GUI freezes when running this code?

http://pastebin.com/620263

I really don't understand. The GUI should run in a seperate thread, and fire events
while the other thread is waiting for them. But since the GUI freezes, nothing happens.

This is a really basic version of what I'm trying to do in my program.

Since this is really urgent I'm awarding 300 points.

Thanks in advance,
Tjeerd Beorman
Comment
Watch Question

Siva Prasanna KumarPrincipal Solutions Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
public class KwartoClient implements ActionListener,Runnable

just try implmenting Runnable class instead of Extending Thread class

Author

Commented:
that didn't change anything
check what happens if You change the line

wait()

to

Thread.sleep(50);

inside a while loop. If that solves the freezed GUI, then You'll have to do it in other way.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Also can You post the code which can be compiled, but with most of the code removed. Even doing that may show You where the real problem is.
In your loop:

while (!madeMove ) {
         wait();
       }

I don't see how the madeMove boolean is going to get changed.

Is there something going on in another thread to set the madeMove to true? If so, are you issuing a notify() or notifyAll() after you set the boolean to true? I'm guessing the wait is hanging until it receives a notify or interrupt.

Commented:
One hint here. If you invoke wait on a certain instance XYZ of an object, you *must*
invoke notify or notifyAll on the *same* instance, XYZ of that object. Otherwise, your
thread will wait indefinitely.

It's better to use a wait with a timeout wait(milliseconds) for handling such situations.
In case if your second thread fails to notify the first thread, your code shouldn't hang.

________
radarsh

Author

Commented:
Okay I think I found the problem:

The game was actually initiated in a GUI class (GameOptions) on pressing of the OK Button. So the game actually
ran inside a function inside a function of that GUI class.

Let me try to initiate the game loop differently.
CERTIFIED EXPERT
Top Expert 2016

Commented:
Something odd here:

>>

actionPerformed(ActionEvent ev) {

    player1.setMove();

  }

>>

.......

>>

 public synchronized void setMove(int move) {

     this.move= move;

     madeMove = true;

     notifyAll();

   }
>>

What happened to the parameter?
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.