懵懵燈燈的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 懵懵燈燈 閱讀(448) 評論(0)  編輯  收藏 所屬分類: Language_Java

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


          網站導航:
           
          主站蜘蛛池模板: 鄂托克旗| 秀山| 太湖县| 株洲市| 军事| 湘阴县| 广宁县| 岳阳市| 夏邑县| 孝感市| 嫩江县| 郓城县| 四子王旗| 武定县| 杂多县| 安化县| 黔西| 布拖县| 分宜县| 永德县| 平邑县| 玉门市| 洞口县| 定边县| 龙井市| 毕节市| 锡林郭勒盟| 宁陵县| 西盟| 肥乡县| 昌邑市| 阳信县| 遵义县| 永川市| 蛟河市| 清徐县| 台安县| 忻州市| 西安市| 博白县| 湘潭县|