成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

淺析Swing線程模型和EDT

開發 后端
最近我用Swing寫一個測試工具,在閱讀我要測試的軟件的codes的時候,發現他在更新UI的時候大量的用到了SwingUtilities的invokelater方法。下面針對Swing線程模型和EDT做些分析。

最近我用Swing寫一個測試工具,在閱讀我要測試的軟件的codes的時候,發現他在更新UI的時候大量的用到了SwingUtilities的invokelater方法。我以前做Swing的應用比較少,大學時代為數不多的幾次寫Swing程序,我記得都是在main方法里面直接創建Frame和更新界面Embarrassed。

以前,我會這么寫:

  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.   }  

所以我仔細搜了一下相關資料,了解到了Swing的單線程模型和EDT(Event-Dispatch-Thread),才發現我原來的做法是非常危險的,遂總結如下:

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. 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.   }  

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

  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. 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中被執行的,所以 一定會等待doHelloWorld方法的執行并返回,即”Hello World on”一定會在”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中被執行的,因而“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

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

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

【編輯推薦】

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

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-10 14:20:01

AcceleratorJava Swing

2009-07-15 11:02:32

Swing組件

2009-07-15 16:50:07

Swing項目

2009-07-16 17:01:09

Swing字符串

2009-07-17 16:41:48

actionPerfoSwing

2009-07-15 13:46:26

Swing體系結構

2009-07-17 11:30:44

Swing Image

2009-07-15 13:48:26

Swing模型和渲染器

2009-07-10 17:24:07

Swing應用程序

2009-07-15 10:06:54

Swing實現MDI

2009-07-15 17:33:08

Swing客戶端

2009-07-14 14:00:47

iData技術

2009-07-16 08:53:03

Swing任務Swing線程

2009-10-16 10:20:37

Python的GIL

2021-06-29 09:34:00

洋蔥模型中間件
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 久久国产综合 | 国产一区二区激情视频 | 国产精品成人一区二区三区夜夜夜 | 美女高潮网站 | 天堂av资源| 羞羞的视频在线看 | 免费一区二区三区 | 午夜视频大全 | 久久久久久久久中文字幕 | 日韩影院在线 | 色综合99| 欧美一区二区小视频 | 天堂色 | 99久久久久久久久 | 性做久久久久久免费观看欧美 | 玖玖免费 | av在线一区二区三区 | 激情五月激情综合网 | 99国产视频 | 久久久久网站 | 久久久久久久久久久久一区二区 | 蜜桃综合在线 | 精品久久香蕉国产线看观看亚洲 | 久久中文一区二区 | 精品国产黄a∨片高清在线 成人区精品一区二区婷婷 日本一区二区视频 | 国产精品国产a级 | 精品国产一二三区 | 一区二区精品 | 久久久免费电影 | 日韩精品在线一区 | 婷婷色网 | 免费国产精品久久久久久 | 国产999精品久久久久久绿帽 | 精品国产精品三级精品av网址 | 91视频.com | 麻豆一区二区三区精品视频 | 综合五月 | 日韩美女爱爱 | 在线观看中文字幕一区二区 | 欧洲一区二区在线 | 波多野结衣一区二区 |