自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺析Swing線程模型和EDT

開發(fā) 后端
最近我用Swing寫一個(gè)測試工具,在閱讀我要測試的軟件的codes的時(shí)候,發(fā)現(xiàn)他在更新UI的時(shí)候大量的用到了SwingUtilities的invokelater方法。下面針對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ì)這么寫:

  1. import java.awt.Color;  
  2. import javax.swing.*;  
  3. public class OldSwingDemo {  
  4.   public static void main(String[] argv) {  
  5.     JLabel bulletin = new JLabel("Hello,World!", JLabel.CENTER);  
  6.     JFrame frame = new JFrame("Bulletin");  
  7.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  8.     frame.getContentPane().add(bulletin);   
  9.     frame.setSize(200150);  
  10.     frame.setVisible(true);  
  11.     bulletin.setForeground(Color.RED);  
  12.   }  

所以我仔細(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,我們先來看一幅圖:

Event_Dispatch_Thread

從上圖我們可以形象的看到,在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)該修改成這樣:

  1. import java.awt.Color;  
  2. import javax.swing.JFrame;  
  3. import javax.swing.JLabel;  
  4. import javax.swing.SwingUtilities;  
  5. public class NewSwingDemo {  
  6.   public static void main(String[] argv) {  
  7.     SwingUtilities.invokeLater(new Runnable() {  
  8.       @Override 
  9.       public void run() {  
  10.         constructUI();  
  11.             }  
  12.     });  
  13.   }  
  14.   private static void 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(200150);  
  20.     frame.setVisible(true);  
  21.     bulletin.setForeground(Color.RED);  
  22.   }  

但是除了線程安全外,還有兩點(diǎn)我們需要注意和理解:

  1. 那種特別耗時(shí)的任務(wù)不應(yīng)該把它放到EDT中,否則這個(gè)應(yīng)用程序會(huì)變得無法響應(yīng)。因?yàn)镋DT會(huì)忙于執(zhí)行你的耗時(shí)的任務(wù),而無暇顧及其他GUI事件。(沒辦法啊,那么多活堆在那,EDT一個(gè)人挑,做男人難啊,做EDT更難!)
  2. 如果你在其他線程訪問和修改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)

  1. public class SwingDemoInvokeAndWait {  
  2.     public static void main(String[] argv) throws InterruptedException, InvocationTargetException {  
  3.  
  4.         SwingUtilities.invokeAndWait(new Runnable() {  
  5.  
  6.             @Override 
  7.             public void run() {  
  8.                 constructUI();  
  9.  
  10.             }  
  11.         });  
  12.  
  13.         final Runnable doHelloWorld = new Runnable() {  
  14.             public void run() {  
  15.  
  16.                 System.out.println("Hello World on " + Thread.currentThread());  
  17.  
  18.             }  
  19.         };  
  20.  
  21.         Thread appThread = new Thread() {  
  22.             public void run() {  
  23.                 try {  
  24.                     SwingUtilities.invokeAndWait(doHelloWorld);  
  25.                 } catch (Exception e) {  
  26.                     e.printStackTrace();  
  27.                 }  
  28.                 System.out.println("Finished on " + Thread.currentThread());  
  29.             }  
  30.         };  
  31.         appThread.start();  
  32.  
  33.     }  
  34.  
  35.     private static void constructUI() {  
  36.         JLabel bulletin = new JLabel("Hello,World!", JLabel.CENTER);  
  37.  
  38.         JFrame frame = new JFrame("Bulletin");  
  39.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  40.         frame.getContentPane().add(bulletin);  
  41.         frame.setSize(200150);  
  42.         frame.setVisible(true);  
  43.         bulletin.setForeground(Color.RED);  
  44.  
  45.     }  

由于doHelloWorld是在invokeAndWait中被執(zhí)行的,所以 一定會(huì)等待doHelloWorld方法的執(zhí)行并返回,即”Hello World on”一定會(huì)在”Finished on”前顯示出來。

(2)

  1. import java.awt.Color;  
  2. import java.lang.reflect.InvocationTargetException;  
  3.  
  4. import javax.swing.JFrame;  
  5. import javax.swing.JLabel;  
  6. import javax.swing.SwingUtilities;  
  7.  
  8. public class SwingDemoInvokeLater {  
  9.     public static void main(String[] argv) throws InterruptedException, InvocationTargetException {  
  10.  
  11.  
  12.         final Runnable doHelloWorld = new Runnable() {  
  13.             public void run() {  
  14.  
  15.                 System.out.println("Hello World on " + Thread.currentThread());  
  16.  
  17.             }  
  18.         };  
  19.  
  20.         Thread appThread = new Thread() {  
  21.             public void run() {  
  22.                 try {  
  23.                     SwingUtilities.invokeLater(doHelloWorld);  
  24.                 } catch (Exception e) {  
  25.                     e.printStackTrace();  
  26.                 }  
  27.                 System.out.println("Finished on " + Thread.currentThread()+",but this might well be displayed before the other message.");  
  28.             }  
  29.         };  
  30.         appThread.start();  
  31.  
  32.     }  
  33.  
  34.     private static void constructUI() {  
  35.         JLabel bulletin = new JLabel("Hello,World!", JLabel.CENTER);  
  36.  
  37.         JFrame frame = new JFrame("Bulletin");  
  38.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  39.         frame.getContentPane().add(bulletin);  
  40.         frame.setSize(200150);  
  41.         frame.setVisible(true);  
  42.         bulletin.setForeground(Color.RED);  
  43.  
  44.     }  

由于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

(4)深入淺出Swing事件分發(fā)線程

原文鏈接:http://www.cnblogs.com/chriswang/archive/2009/09/16/swing-single-thread-queue-mode-and-event-dispatch-thread.html

【編輯推薦】

  1. Swing使用Substance外觀包異常問題
  2. Swing多線程編碼過程中的誤區(qū)
  3. 控件位置可以配置的Swing桌面
  4. Swing特效:漸顯效果
  5. 簡述Java圖形用戶界面設(shè)計(jì)(Swing)
責(zé)任編輯:林師授 來源: Chris Wang的博客
相關(guān)推薦

2009-07-15 16:03:26

Swing線程

2009-07-16 09:54:44

LookupEventSwing線程

2009-07-14 11:30:15

Swing線程

2009-07-10 10:37:50

Swing Set示例

2009-07-14 15:12:36

SwingSWT和AWT

2009-07-15 13:48:26

Swing模型和渲染器

2009-07-10 14:20:01

AcceleratorJava Swing

2009-07-17 16:41:48

actionPerfoSwing

2009-07-15 13:46:26

Swing體系結(jié)構(gòu)

2009-07-15 16:50:07

Swing項(xiàng)目

2009-07-15 11:02:32

Swing組件

2009-07-16 17:01:09

Swing字符串

2009-07-17 11:30:44

Swing Image

2009-07-15 10:06:54

Swing實(shí)現(xiàn)MDI

2009-07-14 14:00:47

iData技術(shù)

2009-07-10 17:24:07

Swing應(yīng)用程序

2009-07-15 17:33:08

Swing客戶端

2009-07-16 08:53:03

Swing任務(wù)Swing線程

2009-10-16 10:20:37

Python的GIL

2014-08-13 10:41:08

linux線程
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)