java gui线程_Java GUI 中的线程安全问题

线程安全问题:

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值