淺析Swing線程模型和EDT
最近我用Swing寫一個測試工具,在閱讀我要測試的軟件的codes的時候,發現他在更新UI的時候大量的用到了SwingUtilities的invokelater方法。我以前做Swing的應用比較少,大學時代為數不多的幾次寫Swing程序,我記得都是在main方法里面直接創建Frame和更新界面Embarrassed。
以前,我會這么寫:
- import java.awt.Color;
- import javax.swing.*;
- public class OldSwingDemo {
- public static void main(String[] argv) {
- 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);
- }
- }
所以我仔細搜了一下相關資料,了解到了Swing的單線程模型和EDT(Event-Dispatch-Thread),才發現我原來的做法是非常危險的,遂總結如下:
Java Swing是一個單線程圖形庫,里面的絕大多數代碼不是線程安全(thread-safe)的,看看Swing各個組件的API,你可以發現絕大多數沒有做同步等線程安全的處理,這意味著它并不是在任何地方都能隨便調用的(假如你不是在做實驗的話),在不同線程里面隨便使用這些API去更新界面元素如設置值,更新顏色很可能會出現問題。
雖然Swing的API不是線程安全,但是如果你按照規范寫代碼(這個規范后面說),Swing框架用了其他方式來保障線程安全,那就是Event Queue和EDT,我們先來看一幅圖:
從上圖我們可以形象的看到,在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這塊就是線程安全的。
所以前面的代碼應該修改成這樣:
- import java.awt.Color;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.SwingUtilities;
- public class NewSwingDemo {
- public static void main(String[] argv) {
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- constructUI();
- }
- });
- }
- 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);
- }
- }
但是除了線程安全外,還有兩點我們需要注意和理解:
- 那種特別耗時的任務不應該把它放到EDT中,否則這個應用程序會變得無法響應。因為EDT會忙于執行你的耗時的任務,而無暇顧及其他GUI事件。(沒辦法啊,那么多活堆在那,EDT一個人挑,做男人難啊,做EDT更難!)
- 如果你在其他線程訪問和修改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)
- 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”前顯示出來。
(2)
- 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”。
參考資料:
(1)Swing Threading and The event-dispatch thread
(2)Section 9.1. Why are GUIs Single-threaded? - Java Concurrency in Practice
(3)How to Use Threads - JFC Swing Tutorial, The: A Guide to Constructing GUIs, Second Edition
【編輯推薦】