Java Event-Dispatch Thread示例程序挂起

时间:2013-07-22 23:10:33

标签: java swing event-dispatch-thread

我在EDT(事件调度线程)javaworld.com上阅读了这篇文章,该文章展示了如何在EDT上正确设置Swing GUI,以及在Runnables中修改GUI的长期运行任务。

然而,所有这些都是有意义的,但是我做的唯一修改示例程序(我粘贴在下面)是Thread.sleep(6000)来模拟长滞后使得界面在几秒钟内没有响应。

我错过了什么吗?

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ViewPage
{

    public static void main(String[] args)
    {
        Runnable r;
        r = new Runnable()
        {
            @Override
            public void run()
            {
                final JFrame frame = new JFrame("View Page");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JPanel panel = new JPanel();
                panel.add(new JLabel("Enter URL"));
                final JTextField txtURL = new JTextField(40);
                panel.add(txtURL);
                frame.getContentPane().add(panel, BorderLayout.NORTH);
                final JTextArea txtHTML = new JTextArea(10, 40);
                frame.getContentPane().add(new JScrollPane(txtHTML),
                        BorderLayout.CENTER);
                ActionListener al;
                al = new ActionListener()
                {
                    public void actionPerformed(ActionEvent ae)
                    {
                        txtURL.setEnabled(false);
                        Runnable worker = new Runnable()
                        {
                            public void run()
                            {
                                InputStream is = null;
                                try
                                {
                                    URL url = new URL(txtURL.getText());
                                    is = url.openStream();
                                    final StringBuilder sb;
                                    sb = new StringBuilder();
                                    int b;
                                    while ((b = is.read()) != -1)
                                    {
                                        sb.append((char) b);
                                    }
                                    Runnable r = new Runnable()
                                    {
                                        public void run()
                                        {
                                            try
                                            {
                                                Thread.sleep(6000);
                                            }
                                            catch (InterruptedException ex)
                                            {
                                                Logger.getLogger(ViewPage.class.getName()).log(Level.SEVERE, null, ex);
                                            }
                                            txtHTML.setText(sb.toString());
                                            txtURL.setEnabled(true);
                                        }
                                    };
                                    try
                                    {
                                        EventQueue.invokeAndWait(r);
                                    }
                                    catch (InterruptedException ie)
                                    {
                                    }
                                    catch (InvocationTargetException ite)
                                    {
                                    }
                                }
                                catch (final IOException ioe)
                                {
                                    Runnable r = new Runnable()
                                    {
                                        public void run()
                                        {
                                            txtHTML.setText(ioe.getMessage());
                                            txtURL.setEnabled(true);
                                        }
                                    };
                                    try
                                    {
                                        EventQueue.invokeAndWait(r);
                                    }
                                    catch (InterruptedException ie)
                                    {
                                    }
                                    catch (InvocationTargetException ite)
                                    {
                                    }
                                }
                                finally
                                {
                                    Runnable r = new Runnable()
                                    {
                                        public void run()
                                        {
                                            txtHTML.setCaretPosition(0);
                                            txtURL.setEnabled(true);
                                        }
                                    };
                                    try
                                    {
                                        EventQueue.invokeAndWait(r);
                                    }
                                    catch (InterruptedException ie)
                                    {
                                    }
                                    catch (InvocationTargetException ite)
                                    {
                                    }
                                    if (is != null)
                                    {
                                        try
                                        {
                                            is.close();
                                        }
                                        catch (IOException ioe)
                                        {
                                        }
                                    }
                                }
                            }
                        };
                        new Thread(worker).start();
                    }
                };
                txtURL.addActionListener(al);
                frame.pack();
                frame.setVisible(true);
            }
        };
        EventQueue.invokeLater(r);
    }
}

1 个答案:

答案 0 :(得分:2)

  

我错过了什么吗?

Runnable只是一个接口而不是另一个线程。 在这一行中,您将打包调用以便稍后在Event Dispatch Thread中执行。

EventQueue.invokeLater(r); // you can use SwingUtilities.invokeLater(r) too 

然后你打电话

Thread.sleep(6000);

这是在EDT中执行的,并且在完成之前不会对gui做出反应。

对于长期任务,你应该使用另一个线程或SwingWorker,并使用一些重复SwingTimer进行短期任务。 阅读Concurrency in Swing