淺析Swing線程模型和EDT
最近我用Swing寫一個(gè)測試工具,在閱讀我要測試的軟件的codes的時(shí)候,發(fā)現(xiàn)他在更新UI的時(shí)候大量的用到了SwingUtilities的invokelater方法。我以前做Swing的應(yīng)用比較少,大學(xué)時(shí)代為數(shù)不多的幾次寫Swing程序,我記得都是在main方法里面直接創(chuàng)建Frame和更新界面Embarrassed。
以前,我會(huì)這么寫:
- 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);
- }
- }
所以我仔細(xì)搜了一下相關(guān)資料,了解到了Swing的單線程模型和EDT(Event-Dispatch-Thread),才發(fā)現(xiàn)我原來的做法是非常危險(xiǎn)的,遂總結(jié)如下:
Java Swing是一個(gè)單線程圖形庫,里面的絕大多數(shù)代碼不是線程安全(thread-safe)的,看看Swing各個(gè)組件的API,你可以發(fā)現(xiàn)絕大多數(shù)沒有做同步等線程安全的處理,這意味著它并不是在任何地方都能隨便調(diào)用的(假如你不是在做實(shí)驗(yàn)的話),在不同線程里面隨便使用這些API去更新界面元素如設(shè)置值,更新顏色很可能會(huì)出現(xiàn)問題。
雖然Swing的API不是線程安全,但是如果你按照規(guī)范寫代碼(這個(gè)規(guī)范后面說),Swing框架用了其他方式來保障線程安全,那就是Event Queue和EDT,我們先來看一幅圖:
從上圖我們可以形象的看到,在GUI界面上發(fā)出的請求事件如窗口移動(dòng),刷新,按鈕點(diǎn)擊,不管是單個(gè)的還是并發(fā)的,都會(huì)被放入事件隊(duì)列(Event Queue)里面進(jìn)行排隊(duì),然后事件分發(fā)線程(Event Dispatch Thread)會(huì)將它們一個(gè)一個(gè)取出,分派到相應(yīng)的事件處理方法。前面我們之所以說Swing是單線程圖形包就是因?yàn)樘幚鞧UI事件的事件分發(fā)線程只有一個(gè),只要你不停止這個(gè)GUI程序,EDT就會(huì)永不間斷去處理請求。
那這種“單線程隊(duì)列模型”的好處是什么呢?在ITPUB的javagui的《深入淺出Swing事件分發(fā)線程》文中總結(jié)了兩點(diǎn):
(1)將同步操作轉(zhuǎn)為異步操作
(2)將并行處理轉(zhuǎn)換為串行順序處理
我覺得還可以補(bǔ)充一點(diǎn):(3)極大地簡化了界面編程。如果是多線程的模型的話,所有事件處理改成異步線程中進(jìn)行,那么界面元素的的同步訪問就要開發(fā)人員自己來做處理,想想也很復(fù)雜,所以也就難怪目前大多數(shù)GUI框架都是采用的是這種單線程的模型。
那我們我們需要注意什么和遵循什么原則呢?
在《JFC Swing Tutorial》中在如何保持“操作GUI代碼線程安全”上做了一個(gè)很好的總結(jié):
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中創(chuàng)建GUI,在事件處理器中修改GUI的,那么你的代碼在Swing這塊就是線程安全的。
所以前面的代碼應(yīng)該修改成這樣:
- 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);
- }
- }
但是除了線程安全外,還有兩點(diǎn)我們需要注意和理解:
- 那種特別耗時(shí)的任務(wù)不應(yīng)該把它放到EDT中,否則這個(gè)應(yīng)用程序會(huì)變得無法響應(yīng)。因?yàn)镋DT會(huì)忙于執(zhí)行你的耗時(shí)的任務(wù),而無暇顧及其他GUI事件。(沒辦法啊,那么多活堆在那,EDT一個(gè)人挑,做男人難啊,做EDT更難!)
- 如果你在其他線程訪問和修改GUI組件,那么你必須要使用SwingUtilities. invokeAndWait(), SwingUtilities. invokeLater() 。他們的倆的都有一個(gè)相同的作用就是將要執(zhí)行的任務(wù)放入事件隊(duì)列(Event Queue)中,好讓EDT允許事件派發(fā)線程調(diào)用另一個(gè)線程中的任意一個(gè)代碼塊。
那么invokeLater()和invokeAndWait()的有什么區(qū)別呢?
單純從字面上來理解public static void invokeLater(Runnable doRun)就是指里面的Runnable運(yùn)行體會(huì)在稍后被調(diào)用運(yùn)行,整個(gè)執(zhí)行是異步的。
public static void invokeAndWait(Runnable doRun)就是指里面定義的Runnable運(yùn)行體會(huì)調(diào)用運(yùn)行并等待結(jié)果返回,是同步的。
下面用兩個(gè)例子來展示他們的區(qū)別:
(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中被執(zhí)行的,所以 一定會(huì)等待doHelloWorld方法的執(zhí)行并返回,即”Hello World on”一定會(huì)在”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中被執(zhí)行的,因而“Finished on”有可能出現(xiàn)在其他信息的前面比如”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
【編輯推薦】