Java UI Hanging with wait()

A

ash

Hello All,

I have a synchronized method that includes a for loop which basically
invoke some methods dynamically from other classes and then highlight
some text in a JTextArea then it waits for 1000 ms (this.wait(1000)).
The class that includes this synchronized method extends Object.


The problem is that when i press the button that invokes this method
everything in the GUI hangs until the method finishes.But every other
thing than the GUI works normally.

What is supposed to happen is that every thing works normally and
then the caller object sleeps for 1000 ms , wakes up and continue ....



---Code snippet begin --

for(Method m : algClass.getDeclaredMethods())
{
if(m.toString().endsWith("Do()"))
{
methodStack.push(m);
algViewArea.eraseAllHighLights();

algViewArea.highLightLine(((Algorithm)classLoadedObject).getCurrentLine());
m.invoke(classLoadedObject, (Object[]) null);
this.wait(1000);

}
}

--Code snipper End--

everything works fine except the following :
1) algViewArea.eraseAllHighLights();
2)
algViewArea.highLightLine(((Algorithm)classLoadedObject).getCurrentLine());

And in general the whole GUI hangs ...




What am i missing ?


Thanx in advance for your help :D


Ahmed Ashmawy
 
K

Knute Johnson

ash said:
Hello All,

I have a synchronized method that includes a for loop which basically
invoke some methods dynamically from other classes and then highlight
some text in a JTextArea then it waits for 1000 ms (this.wait(1000)).
The class that includes this synchronized method extends Object.


The problem is that when i press the button that invokes this method
everything in the GUI hangs until the method finishes.But every other
thing than the GUI works normally.

What is supposed to happen is that every thing works normally and
then the caller object sleeps for 1000 ms , wakes up and continue ....



---Code snippet begin --

for(Method m : algClass.getDeclaredMethods())
{
if(m.toString().endsWith("Do()"))
{
methodStack.push(m);
algViewArea.eraseAllHighLights();

algViewArea.highLightLine(((Algorithm)classLoadedObject).getCurrentLine());
m.invoke(classLoadedObject, (Object[]) null);
this.wait(1000);

}
}

--Code snipper End--

everything works fine except the following :
1) algViewArea.eraseAllHighLights();
2)
algViewArea.highLightLine(((Algorithm)classLoadedObject).getCurrentLine());

And in general the whole GUI hangs ...




What am i missing ?


Thanx in advance for your help :D


Ahmed Ashmawy

You are blocking the Event Dispatch Thread. Nothing GUI related will
work while its thread is asleep.

If you need to do two things at different times to your GUI, run another
thread that calls the actions on the GUI in the EDT using
EventQueue.invokeLater().
 
A

ash

You are blocking the Event Dispatch Thread. Nothing GUI related will
work while its thread is asleep.

If you need to do two things at different times to your GUI, run another
thread that calls the actions on the GUI in the EDT using
EventQueue.invokeLater().


I created a thread that is passed the components that will be modified
in the GUI and its run() it modified it.

That thread is called by EventQueue.invokeLater() method from the
synchronized method. It still didnt work , it did the same behaviour
exactly. I suspect i am doing it incorrectly. if you would point out
whats done incorrectly i would greatly appreciate it.
 
A

ash

You are blocking the Event Dispatch Thread. Nothing GUI related will
work while its thread is asleep.
If you need to do two things at different times to your GUI, run another
thread that calls the actions on the GUI in the EDT using
EventQueue.invokeLater().

I created a thread that is passed the components that will be modified
in the GUI and in its run() it modified these passed components.

That thread is called by EventQueue.invokeLater() method from the
synchronized method. It still didnt work , it did the same behaviour
exactly. I suspect i am doing it incorrectly. if you would point out
whats done incorrectly i would greatly appreciate it.
 
E

Eric Sosman

ash said:
I created a thread that is passed the components that will be modified
in the GUI and its run() it modified it.

That thread is called by EventQueue.invokeLater() method from the
synchronized method. It still didnt work , it did the same behaviour
exactly. I suspect i am doing it incorrectly. if you would point out
whats done incorrectly i would greatly appreciate it.

As Knute said, you're blocking the EDT. invokeLater()
runs your code *on* the EDT, meaning that the EDT cannot
do anything else -- like keeping the GUI alive -- until
your code finishes.

Run your code on its own thread, not on the EDT. When
your code needs to update the GUI, use invokeLater() to
get the updating -- and only the updating -- to run on
the EDT.

See the Java Tutorial for examples of how to handle
long-running tasks in a GUI framework.

http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html
 
K

Knute Johnson

ash said:
I created a thread that is passed the components that will be modified
in the GUI and its run() it modified it.

That thread is called by EventQueue.invokeLater() method from the
synchronized method. It still didnt work , it did the same behaviour
exactly. I suspect i am doing it incorrectly. if you would point out
whats done incorrectly i would greatly appreciate it.

Here is a simple example you can try.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class test7 {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

c.gridx = 1;
final JLabel l = new JLabel("Button Not Yet Pressed");
f.add(l,c);

final JButton b = new JButton("Start");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
b.setEnabled(false);
l.setText("Running");
Runnable r = new Runnable() {
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
b.setEnabled(true);
l.setText("Done");
}
});
}
};
new Thread(r).start();
}
});
f.add(b,c);

f.pack();
f.setVisible(true);
}
});
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top