Swing线程安全编程

6
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(MyDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(MyDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(MyDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(MyDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the dialog */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            MyDialog dialog = new MyDialog(new javax.swing.JFrame(), true);
            dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                @Override
                public void windowClosing(java.awt.event.WindowEvent e) {
                    System.exit(0);
                }
            });
            dialog.setVisible(true);
        }
    });
}

MyDialog类只有几个组合框和文本字段,我使用DB值填充组合框。选择一个组合框值后,我从数据库中获取另一个值以填充下一个组合框。
不使用invokeLater线程,上述程序将以相同的方式运行。在Swing编程中,何时使用invokeLater会变得有用?我已经读了一些关于它的理论知识,但似乎都是理论性的。invokeLater对应用程序有什么区别?仅在main方法内部使用它是否足够,还是也应在操作监听器中使用它?
SwingUtilities.invokeLater和java.awt.EventQueue.invokeLater - 它们是相同的吗?

6
你有没有阅读过Swing线程官方文档?链接为http://docs.oracle.com/javase/tutorial/uiswing/concurrency/。 - Guillaume
6
它能够正常工作是因为你很幸运。由于线程时序引起的竞态条件通常几乎不可能可靠地再现。代码似乎正常工作,但同样的代码可能在其他系统或不同的JVM上出现问题并不是不寻常的。程序越大,出现问题的可能性就越高。SwingUtilities.invokeLater() 调用了 java.awt.EventQueue.invokeLater() - kiheru
4
大多数事件通常在EDT上发生,但并非全部。您可以始终使用SwingUtilities.isEventDispatchThread()来检查此内容。必须始终注意,就像在此示例中所做的那样,在displaySelectionInfo(...)方法内部进行查看,并请阅读相同位置上方的注释。希望这给您留下了关于整个事情的大致印象 :-) - nIcE cOw
4
刚开始使用Swing时,我吃了个苦头。我完全忽略了线程安全,虽然我的应用程序在Windows XP上看起来很好,但在Vista上完全崩溃了。之后我和SwingUtilities.invokeLater()成为了好朋友。 :-) - perp
1个回答

6
没有理论内容,这很实用。 SwingUtilities.invokeLater() 方法保证 Runnable 中的代码将在 Event Dispatch Thread (EDT) 上运行。这很重要,因为 Swing 不是线程安全的,因此与 GUI(Swing 等)相关的任何内容都需要在 EDT 上运行。 EDT 是一个“它发生的时候就发生”的线程,它不保证按照执行顺序执行。如果 GUI 代码在后台线程中执行(比如在一个 SwingWorker 实例中),那么它可能会抛出错误。我通过自己的学习经历了解到这一点:在我的学习年代里,在后台线程中执行更改 GUI 的代码会导致随机、不一致的 RuntimeException,我无法解决。这是一个很好的学习经验(SwingWorker 有一个用于后台任务的 doInBackground() 方法和一个用于 EDT 任务的 done() 方法)。
同样地,您也不希望在 EDT 上执行大型操作(数据库查询等)。这是因为 EDT 正在调度所有 GUI 事件,因此 EDT 上的所有内容都应该非常简短和简洁。您可以通过将 JProgressBar 设置为不确定来轻松查看这一点。
此 SSCCE 应该很好地说明了它。请注意,当调用 method() 时,JProgressBar 的动作会发生变化,一次在后台线程上,一次在 EDT 线程上。
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;

/**
 *
 * @author Ryan
 */
public class Test {

    public static void main(String args[]) {
        JFrame frame = new JFrame();
        JProgressBar jpb = new JProgressBar();
        jpb.setIndeterminate(true);
        frame.add(jpb);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        new Task().execute();
    }

    public static void method() { // This is a method that does a time-consuming task.
        for(int i = 1; i <= 5; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(i);
        }
    }

    static class Task extends SwingWorker<Void, Void> {

        @Override
        protected Void doInBackground() throws Exception {
            /* Executing method on background thread.
             * The loading bar should keep moving because, although method() is time consuming, we are on a background thread.
            */ 
            method();
            return null;
        }

        @Override
        protected void done() {
            /* Executing method on Event Dispatch Thread.
             * The loading bar should stop because method() is time consuming and everything on the Event Dispatch Thread
             * (like the motion of the progress bar) is waiting for it to finish.
            */

            // 
            method();
        }
    }
}

希望这能有所帮助。

不错的例子。但对我来说,大多数要求是只有在当前操作完成后才能继续执行其他操作。在这样的应用程序中,将工作放在后台线程中执行是否有帮助,而不是在EDT中执行?无论它是否在EDT中,我都希望用户只有在当前操作完成后才能进行进一步的操作。因此,我可以向他显示一个进度条。当我在动作监听器和从动作监听器调用的方法中执行所有操作时,它只在EDT上执行。根本不需要Swing Worker。如果我错了,请纠正我。 - Mohamed Iqzas
在这种情况下,如果您需要用户交互(“确定”,“取消”等),那么您可能需要将多个SwingWorker链接在一起。因此,您可以获得初始输入,然后在后台线程上执行代码,然后请求更多输入,然后启动另一个后台线程,依此类推。这将需要多个SwingWorker。在这种情况下,我会尝试聪明地获取用户输入(即,您是否可以提前获取所有用户输入?) - ryvantage
是的。用户将在单个操作中提供该特定窗口/对话框的所有用户输入。然后根据操作显示另一个窗口或结果。让我直接问:SwingWorker仅在用户执行其他操作时执行后台任务是必要的吗? - Mohamed Iqzas
1
是的,除非您不使用Swing。如果您需要使用Swing组件来启动、报告、停止或以其他方式与需要在后台线程上运行的长时间进程进行交互,SwingWorker是您唯一可以轻松管理代码在EDT还是后台线程上执行的选项。 - ryvantage

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接