懵懵燈燈的BLOG

          寒夜孤燈點(diǎn)點(diǎn)星

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            56 隨筆 :: 10 文章 :: 22 評(píng)論 :: 0 Trackbacks
          線程安全問題:

          From:http://www.jguru.com/faq/view.jsp?EID=131370


          Question - Please explain why Swing is not thread safe and AWT is.

          Answer
          Simple answer is - "that's the design choice the Swing team made". It is a well-known fact that writing thread safe API/library is more difficult and inefficient.

          So to simplify the implementation of Swing library they chose it to be not thread safe. The argument being that most of the GUI related work happens in the callbacks from the GUI which happen on the single GUI thread anyways. Granted - for long running tasks the user will have to do more work if he/she wants to do multithreaded activity. Not making Swing thread safe allowed them to implement the Swing which covered a lot more ground (new controls, layouts, keyboard actions, layered pane etc) in a short amount of time.

          It is not that bad though - Swing does provide a mechanism to deal with the issues of threading -

          • javax.swing.SwingUtilities.invokeLater(Runnable ...);
          • javax.swing.SwingUtilities.invokeAndWait(Runnable ...);
          • javax.swing.JProgressBar class
          • javax.swing.ProgressMonitor
          • javax.swing.ProgressMonitorInputStream
          • SwingWorker

          For more explaination of why they made that decision please see the following URLs:

          The AWT is based on the OS's WIndowing System's peer objects which are inherently thread safe. That is why AWT is thread safe.

          One can argue though that they should have provided factory methods (similar to collections framework) or subclasses to get thread safe versions of the Swing classes - for example, TSJTextField or TSJTree where the "TS" stands for 'thread safe'


          另外 , swt和swing一樣都是線程不安全的, 但是java.util.Timer確是安全的,

          thread safe問題歸根結(jié)底是該類和方法是否可重入,re-entrance.

          posted on 2008-05-14 20:42 懵懵燈燈 閱讀(452) 評(píng)論(0)  編輯  收藏 所屬分類: Language_Java

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 张家界市| 离岛区| 大渡口区| 平定县| 石柱| 浙江省| 清水河县| 堆龙德庆县| 舟山市| 天峨县| 花莲市| 玉田县| 利津县| 喜德县| 苍梧县| 东方市| 鄱阳县| 清流县| 南投县| 渭源县| 巴东县| 肥乡县| 榆中县| 德钦县| 南丰县| 定兴县| 景洪市| 临泉县| 洪洞县| 高要市| 临潭县| 富锦市| 宜良县| 准格尔旗| 金华市| 乐山市| 会泽县| 泾源县| 思茅市| 晋宁县| 麻城市|