invokeLater和invokeAndWait

invokeLater和invokeAndWait的区别

invokeLater:在把可运行的对象放入队列后就返回,而invokeAndWait一直等待已启动了可运行的run方法才返回。如果一个操作在另外一个操作执行前必须从一个组件获得信息,则invokeAndWait方法很有用的。

    public class SwingDemoInvokeAndWait {  
        public static void main(String[] argv) throws InterruptedException, InvocationTargetException {  

            SwingUtilities.invokeAndWait(new Runnable() {  

                @Override 
                public void run() {  
                    constructUI();  

                }  
            });  

            final Runnable doHelloWorld = new Runnable() {  
                public void run() {  

                    System.out.println("Hello World on " + Thread.currentThread());  

                }  
            };  

            Thread appThread = new Thread() {  
                public void run() {  
                    try {  
                        SwingUtilities.invokeAndWait(doHelloWorld);  
                    } catch (Exception e) {  
                        e.printStackTrace();  
                    }  
                    System.out.println("Finished on " + Thread.currentThread());  
                }  
            };  
            appThread.start();  

        }  

        private static void constructUI() {  
            JLabel bulletin = new JLabel("Hello,World!", JLabel.CENTER);  

            JFrame frame = new JFrame("Bulletin");  
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
            frame.getContentPane().add(bulletin);  
            frame.setSize(200, 150);  
            frame.setVisible(true);  
            bulletin.setForeground(Color.RED);  

        }  
    } 

由于doHelloWorld是在invokeAndWait中被执行的,所以 一定会等待doHelloWorld方法的执行并返回,即”Hello World on”一定会在”Finished on”前显示出来。

    import java.awt.Color;  
    import java.lang.reflect.InvocationTargetException;  

    import javax.swing.JFrame;  
    import javax.swing.JLabel;  
    import javax.swing.SwingUtilities;  

    public class SwingDemoInvokeLater {  
        public static void main(String[] argv) throws InterruptedException, InvocationTargetException {  


            final Runnable doHelloWorld = new Runnable() {  
                public void run() {  

                    System.out.println("Hello World on " + Thread.currentThread());  

                }  
            };  

            Thread appThread = new Thread() {  
                public void run() {  
                    try {  
                        SwingUtilities.invokeLater(doHelloWorld);  
                    } catch (Exception e) {  
                        e.printStackTrace();  
                    }  
                    System.out.println("Finished on " + Thread.currentThread()+",but this might well be displayed before the other message.");  
                }  
            };  
            appThread.start();  

        }  

        private static void constructUI() {  
            JLabel bulletin = new JLabel("Hello,World!", JLabel.CENTER);  

            JFrame frame = new JFrame("Bulletin");  
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
            frame.getContentPane().add(bulletin);  
            frame.setSize(200, 150);  
            frame.setVisible(true);  
            bulletin.setForeground(Color.RED);  

        }  
    } 

由于doHelloWorld是在invokeLater中被执行的,因而“Finished on”有可能出现在其他信息的前面比如”Hello World On”。

参考文章:http://developer.51cto.com/art/201201/313034.htm
http://blog.csdn.net/legendmohenote/article/details/5853833
http://blog.csdn.net/yanwushu/article/details/39434159

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值