懵懵燈燈的BLOG

          寒夜孤燈點點星

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            56 隨筆 :: 10 文章 :: 22 評論 :: 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問題歸根結底是該類和方法是否可重入,re-entrance.

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

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 鹤岗市| 三穗县| 岢岚县| 额敏县| 喜德县| 丰原市| 临武县| 大姚县| 忻城县| 开远市| 长子县| 赣州市| 德保县| 马龙县| 卓尼县| 柏乡县| 武清区| 岚皋县| 麻城市| 鹰潭市| 红桥区| 西宁市| 舞钢市| 遂川县| 兰溪市| 浦东新区| 宾阳县| 绩溪县| 梅河口市| 承德县| 英吉沙县| 大港区| 谢通门县| 册亨县| 敖汉旗| 辽中县| 舟山市| 铅山县| 定南县| 肃南| 共和县|