Why does my GUI still hang even after using SwingUtilities.invokeLater?
Asked Answered
D

5

7

I have this ActionListener that gets called in the EDT. My plot() function is computationally heavy, it can easily take five seconds. It made the GUI hang as expected. I added the SwingUtilities.invokeLater code and it still hangs. Shouldn't the GUI be responsive now that I am spawning a separate thread for my heave computation?

final ActionListener applyListener = new ActionListener() 
        {
            @CommitingFunction
            public void actionPerformed(ActionEvent arg0) 
            {
                /*Don't do plotting in the EDT :)*/
                SwingUtilities.invokeLater(new Runnable() 
                {
                    public void run() 
                    {
                        plot();
                    }
                });
            }
        };
Diehard answered 19/11, 2010 at 17:34 Comment(0)
A
16

Not at all. InvokeLater is not producing a new thread. invokeLater exists to tell Swing explicitly "use the Event Dispatching Thread for this, but not right now". invoke and invokeLater exist to let you do operations that are only safe for the Event Dispatching Thread from other threads- not by doing them on those threads, but by telling the EDT to do them.

Your ActionListener will run very quickly, throwing the Runnable on Swing's event dispatching queue. Then when it gets that far, it will take five seconds to run the plot().

The only workaround is to refactor plot(). Use a SwingWorker (or similar multithreading strategy, but SwingWorker is probably the best for this) to actually move the logic of plot() onto a different thread. That thread cannot safely draw anything because it is not the Swing Event Dispatching Thread, so all of its draw operations need to be performed via invokeLater(). For efficiency reasons, you should try to do all of the drawing operations at once, on one invokeLater(), using results stored from your calculation.

Auvergne answered 19/11, 2010 at 17:42 Comment(2)
So do I want to make the plot() code run in a class that extends SwingWorker, and run that computationally heavy code in the doInBackground() function and then use the done() function to refresh my display? Will my GUI still be active while the computation is taking place?Diehard
Bingo. That's what SwingWorker does and is for.Auvergne
E
4

You're doing the opposite of what you think you are. Instead of running your computation thread outside of the EDT, you're explicitly calling it within it!

SwingUtilities.invokeLater() queues up the runnable for invocation at a later time, in the EDT! You want to use SwingWorker instead.

Ephebe answered 19/11, 2010 at 17:43 Comment(0)
C
2

You don't show what is inside the plot() function, but you should not put any painting in there. Compute whatever you want in the new thread, and do the painting in the EDT. To do this, it is better to use SwingWorker

Cacilia answered 19/11, 2010 at 17:43 Comment(0)
C
1

invokeLater add a task to the GUIs work queue. It will be invoked after all other tasks have been performed, however it still uses the GUI thread.

I suggest you look at using an ExecutorService.

As @Adam suggests, any actual drawing it does needs to be done via invokeLater.

Catherine answered 19/11, 2010 at 17:42 Comment(0)
D
1

Here is what I did for my company's app, this is some pseudo code because of legal reasons, but the jist of it is that if the screen is unresponsive, it will reboot the GUI. Whenever you use SwingUtilities to kick off the EDT, in that same init block, create two watcher threads. One thread will simply perform an action on the EDT thread using Swing utilities. Another thread will monitor the first thread to see if feels the first thread is responsive. The first thread will only acknowledge responsiveness if it can perform a very simple command.

set isEDTCheck to true when running in normal fashion, false in debug mode (otherwise you'll constantly get rebooted.

    if (isEDTCheck) {
        new Thread("EDTHeartbeat") {
            @Override
            public void run() {
                Runnable thisThingYouDo = new Runnable() {
                    public void run() {
                        int x = 0;
                    }
                };
                while (true) {
                    // first thread says we are waiting, aka bad state
                    edtwait=true;
                    try {
                        javax.swing.SwingUtilities.invokeAndWait(thisThingYouDo);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    // first thread says we are not waiting, good state
                    edtwait=false;
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        new Thread("EDTValidator") {
            @Override
            public void run() {
                while (true) {
                    // is first thread in bad state?
                    if (edtwait) {
                        try {
                            Thread.sleep(3000);
                            // after 3 seconds are we still in bad state?  if so, get rid of initial frame, pop up a dialog box in AWT that does no commands
                            if (edtwait) {
                                mainFrame.setVisible(false);
                                new Dialog();  
                            } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }


  public class Dialog extends Frame {
    private static final int WIDTH = 400;
    private static final int HEIGHT = 300;
    Frame f = null;
    public Dialog() {
        f = this;
        hasSomethingBeenEntered=false;
        this.setTitle("APP PROBLEM DETECTED");
        this.setSize(WIDTH, HEIGHT);
        this.setLocation((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth() - myapp.width, 0);
        Panel p1 = new Panel() {
            @Override
            public void paint(final Graphics g) {
                int left = Dialog.WIDTH/2 - 45; // don't use WIDTH shadowed by Panel class
                int top = Dialog.HEIGHT/2 - 20; // same as above
                g.drawString("APP HAS DETECTED A PROBLEM", left, top);
            }
        };
        this.add("Center", p1);

        this.setAlwaysOnTop(true);
                 TextArea tb = new TextArea("APP HAS DETECTED A MAJOR PROBLEM\nIT WILL NOW RESTART IN 5 SECONDS");
        this.add(tb);
        this.setVisible(true);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        restartApp();

    }

    private void restartApp() {
            Runtime.getRuntime().exec("cmd /c start cmd.exe /K \"cd C:\\Progra~1\\Common~1 && C:\\Progra~1\\Common~1\\MyAppDir\\myjavaapp.jar\"");
            System.exit(0);
       }
Dahlia answered 2/8, 2012 at 19:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.