swingEDT机制

Java Swing是一个单线程图形库,里面的绝大多数代码不是线程安全(thread-safe)的,看看Swing各个组件的API,你可以发现绝大多数没有做同步等线程安全的处理,这意味着它并不是在任何地方都能随便调用的(假如你不是在做实验的话),在不同线程里面随便使用这些API去更新界面元素如设置值,更新颜色很可能会出现问题。

虽然Swing的API不是线程安全,但是如果你按照规范写代码(这个规范后面说),Swing框架用了其他方式来保障线程安全,那就是Event Queue和EDT,我们先来看一幅图:

Event_Dispatch_Thread

从上图我们可以形象的看到,在GUI界面上发出的请求事件如窗口移动,刷新,按钮点击,不管是单个的还是并发的,都会被放入事件队列(Event Queue)里面进行排队,然后事件分发线程(Event Dispatch Thread)会将它们一个一个取出,分派到相应的事件处理方法。前面我们之所以说Swing是单线程图形包就是因为处理GUI事件的事件分发线程只有一个,只要你不停止这个GUI程序,EDT就会永不间断去处理请求。

那这种“单线程队列模型”的好处是什么呢?在ITPUB的javagui的《深入浅出Swing事件分发线程》文中总结了两点:

(1)将同步操作转为异步操作

(2)将并行处理转换为串行顺序处理

我觉得还可以补充一点:(3)极大地简化了界面编程。如果是多线程的模型的话,所有事件处理改成异步线程中进行,那么界面元素的的同步访问就要开发人员自己来做处理,想想也很复杂,所以也就难怪目前大多数GUI框架都是采用的是这种单线程的模型。

那我们我们需要注意什么和遵循什么原则呢?

在《JFC Swing Tutorial》中在如何保持“操作GUI代码线程安全”上做了一个很好的总结:

To avoid the possibility of deadlock, you must take extreme care that Swing components and models are modified or queried only from the event-dispatching thread. As long as your program creates its GUI from the event-dispatching thread and modifies the GUI only from event handlers, it is thread safe.

只要你是在EDT中创建GUI,在事件处理器中修改GUI的,那么你的代码在Swing这块就是线程安全的。

所以前面的代码应该修改成这样:

 
 
  1. import java.awt.Color;  

  2. import javax.swing.JFrame;  

  3. import javax.swing.JLabel;  

  4. import javax.swing.SwingUtilities;  

  5. publicclass NewSwingDemo {  

  6. publicstaticvoid main(String[] argv) {  

  7.    SwingUtilities.invokeLater(new Runnable() {  

  8. @Override

  9. publicvoid run() {  

  10.        constructUI();  

  11.            }  

  12.    });  

  13.  }  

  14. privatestaticvoid constructUI() {  

  15.    JLabel bulletin = new JLabel("Hello,World!", JLabel.CENTER);  

  16.    JFrame frame = new JFrame("Bulletin");  

  17.    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

  18.    frame.getContentPane().add(bulletin);  

  19.    frame.setSize(200, 150);  

  20.    frame.setVisible(true);  

  21.    bulletin.setForeground(Color.RED);  

  22.  }  

  23. }

但是除了线程安全外,还有两点我们需要注意和理解:

  1. 那种特别耗时的任务不应该把它放到EDT中,否则这个应用程序会变得无法响应。因为EDT会忙于执行你的耗时的任务,而无暇顾及其他GUI事件。(没办法啊,那么多活堆在那,EDT一个人挑,做男人难啊,做EDT更难!)

  2. 如果你在其他线程访问和修改GUI组件,那么你必须要使用SwingUtilities. invokeAndWait(), SwingUtilities. invokeLater() 。他们的俩的都有一个相同的作用就是将要执行的任务放入事件队列(Event Queue)中,好让EDT允许事件派发线程调用另一个线程中的任意一个代码块。

那么invokeLater()和invokeAndWait()的有什么区别呢?

单纯从字面上来理解public static void invokeLater(Runnable doRun)就是指里面的Runnable运行体会在稍后被调用运行,整个执行是异步的。

public static void invokeAndWait(Runnable doRun)就是指里面定义的Runnable运行体会调用运行并等待结果返回,是同步的。

下面用两个例子来展示他们的区别:

(1)

 
 
  1. publicclass SwingDemoInvokeAndWait {  

  2. publicstaticvoid main(String[] argv) throws InterruptedException, InvocationTargetException {  

  3.        SwingUtilities.invokeAndWait(new Runnable() {  

  4. @Override

  5. publicvoid run() {  

  6.                constructUI();  

  7.            }  

  8.        });  

  9. final Runnable doHelloWorld = new Runnable() {  

  10. publicvoid run() {  

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

  12.            }  

  13.        };  

  14.        Thread appThread = new Thread() {  

  15. publicvoid run() {  

  16. try {  

  17.                    SwingUtilities.invokeAndWait(doHelloWorld);  

  18.                } catch (Exception e) {  

  19.                    e.printStackTrace();  

  20.                }  

  21.                System.out.println("Finished on " + Thread.currentThread());  

  22.            }  

  23.        };  

  24.        appThread.start();  

  25.    }  

  26. privatestaticvoid constructUI() {  

  27.        JLabel bulletin = new JLabel("Hello,World!", JLabel.CENTER);  

  28.        JFrame frame = new JFrame("Bulletin");  

  29.        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

  30.        frame.getContentPane().add(bulletin);  

  31.        frame.setSize(200, 150);  

  32.        frame.setVisible(true);  

  33.        bulletin.setForeground(Color.RED);  

  34.    }  

  35. }

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

(2)

 
 
  1. import java.awt.Color;  

  2. import java.lang.reflect.InvocationTargetException;  

  3. import javax.swing.JFrame;  

  4. import javax.swing.JLabel;  

  5. import javax.swing.SwingUtilities;  

  6. publicclass SwingDemoInvokeLater {  

  7. publicstaticvoid main(String[] argv) throws InterruptedException, InvocationTargetException {  

  8. final Runnable doHelloWorld = new Runnable() {  

  9. publicvoid run() {  

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

  11.            }  

  12.        };  

  13.        Thread appThread = new Thread() {  

  14. publicvoid run() {  

  15. try {  

  16.                    SwingUtilities.invokeLater(doHelloWorld);  

  17.                } catch (Exception e) {  

  18.                    e.printStackTrace();  

  19.                }  

  20.                System.out.println("Finished on " + Thread.currentThread()+",but this might well be displayed before the other message.");  

  21.            }  

  22.        };  

  23.        appThread.start();  

  24.    }  

  25. privatestaticvoid constructUI() {  

  26.        JLabel bulletin = new JLabel("Hello,World!", JLabel.CENTER);  

  27.        JFrame frame = new JFrame("Bulletin");  

  28.        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

  29.        frame.getContentPane().add(bulletin);  

  30.        frame.setSize(200, 150);  

  31.        frame.setVisible(true);  

  32.        bulletin.setForeground(Color.RED);  

  33.    }  

  34. }

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


你可能感兴趣的:(java,程序,元素,模型,先来)