Re: [問題] EventQueue.invokeLater 使用疑問

看板java作者 (十年一夢)時間10年前 (2015/04/29 22:00), 10年前編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/3 (看更多)
※ 引述《noapaov (單身漢)》之銘言: : 標題: [問題] EventQueue.invokeLater 使用疑問 : 時間: Wed Apr 29 11:59:43 2015 : : : 請教一下各位, 目前在Swing碰到一個疑問, : : 看了大多數的書籍, 在使用Swing建議使用方法如下 : public static void main(String args[]) { : : java.awt.EventQueue.invokeLater(new Runnable() { : public void run() { : : System.out.println("test"); : new NewJFrame().setVisible(true); : } : }); : } : : 也就是視窗程式會開一個thread來處理Event Queue和GUI的事情, : : 但我是用下列方法一樣會產生該視窗物件, 也沒發生什麼錯誤 : : public static void main(String args[]) { : new NewJFrame().setVisible(true); : : } : : 想請問各位大大, 這兩著到底差別在哪? 謝謝 : : -- : ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 203.69.59.91 : ※ 文章網址: https://www.ptt.cc/bbs/java/M.1430279986.A.363.html : 推 mars90226: 把GUI跟處理程式主要功能的thread分開,這樣你就不會 04/29 12:04 : → mars90226: 在處理事情的時候,GUI看起來像是沒有回應 04/29 12:05 : → mars90226: 你如果沒做甚麼花很多時間的事情,就會沒甚麼差別 04/29 12:05 推文講的觀念本身是正確的,是每個搞 GUI 的人都需要知道的事,但是此帖主要 是關於為什麼需要把 UI component 的建立與操作放到 UI thread 裡*,推文回覆 的就不是那麼貼切。 swing 的文件裡有提到 swing 設計上除了少數幾個特定的 methods 外(repaint, setText...),大部分都不是 thread-safe,也就是說同時**在多個 thread 裡各自 對同一 swing component 進行操作,不保證能維持 component(object) 的 invariant 性質(component 不一定會處在一個合理的狀態)。 就理論上來說,不知道 swing component 的 constructor 確切做了什麼事(有沒有 把自身的 reference 擴散到 UI thread 裡,有沒有操作(或被操作)其他的 swing component 了等等),保險的做法是從建構 swing component 這一動作開始就排程到 UI thread 去做,就能確保所有的 swing component 會處在一種正確合理的狀態。 至於直接在 main thread 執行這樣的操作,你說也沒有錯誤: new NewJFrame().setVisible(true); 比較保守的看法是: 1. 某些 swing component 已經爛掉了,但是你還沒有(或沒辦法)觀察到。 2. 你運氣好,當次執行這個程式後,確實是沒有任何 swing component 爛掉。 比較大膽的看法是: 在你使用的 JRE 所帶的 core classes implementation 之下,在 main thread 裡做這些操作本來就(剛好)總是不會有問題的。 有人爭論過到底可不可(安全地)在 main thread 裡建構 main frame(window) 以及 初始化的操作,這部分我不想評論。 *直到 Java 8(1.8) 的 API doc,EventQueue.invokeLater/invokeAndWait 部分都 沒有明講是把 Runnable 排到 UI thread 裡去執行,但是 SwingUtilities 裡同名 的兩個 method 則有明確提到是把 Runnable 排到 UI thread(event dispatching thread)裡去執行,且 SwingUtilities 的實作只是 cover for java.awt.EventQueue.invokeLater()/java.awt.EventQueue.invokeAndWait()。 **這裏的"同時"意指在某個 thread A invoke component C 的 methodA 到 methodA return 的這段時間,與某個 thread B invoke 同一 Component C 的 methodB 到 methodB return 的這段時間,兩者有重疊的部分。 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.238.70.213 ※ 文章網址: https://www.ptt.cc/bbs/java/M.1430316014.A.1CB.html ※ 編輯: sbrhsieh (36.238.70.213), 04/29/2015 22:05:51
文章代碼(AID): #1LGEFk7B (java)
文章代碼(AID): #1LGEFk7B (java)