淺析Swing執行緒模式和EDT

來源:互聯網
上載者:User

   最近我用Swing寫一個測試載入器,在閱讀我要測試的軟體的codes的時候,發現他在更新UI的時候大量的用到了SwingUtilities的invokelater方法。我以前做Swing的應用比較少,大學時代為數不多的幾次寫Swing程式,我記得都是在main方法裡面直接建立Frame和更新介面。

   以前,我會這麼寫:

Code
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這塊就是安全執行緒的。

所以前面的代碼應該修改成這樣:

Code
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);



  }


}

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

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)

Code
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)

Code
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

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.