Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2008

Tip: Looking for answers? Try searching our database.

Java UI Hanging with wait()

Thread view: 
ash - 17 Mar 2008 19:28 GMT
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
Knute Johnson - 17 Mar 2008 19:52 GMT
> Hello All,
>
[quoted text clipped - 40 lines]
>
> 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().

Signature

Knute Johnson
email s/nospam/linux/

     ------->>>>>>http://www.NewsDem

ash - 17 Mar 2008 21:10 GMT
> 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.
Eric Sosman - 17 Mar 2008 21:24 GMT
>> You are blocking the Event Dispatch Thread.  Nothing GUI related will
>> work while its thread is asleep.
[quoted text clipped - 10 lines]
> 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

Signature

Eric.Sosman@sun.com

Knute Johnson - 17 Mar 2008 22:11 GMT
>> You are blocking the Event Dispatch Thread.  Nothing GUI related will
>> work while its thread is asleep.
[quoted text clipped - 10 lines]
> 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);
            }
        });
    }
}

Signature

Knute Johnson
email s/nospam/linux/

ash - 17 Mar 2008 22:51 GMT
Thank you , great help :D
ash - 17 Mar 2008 21:11 GMT
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.