它是安全的构建在事件指派线程不可摆动/ AWT小部件? [英] Is it safe to construct Swing/AWT widgets NOT on the Event Dispatch Thread?

查看:228
本文介绍了它是安全的构建在事件指派线程不可摆动/ AWT小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经整合物质的外观和感觉在我的应用程序,并遇到了一些问题关于它的内部EDT(事件调度线程)检查程序。物质绝对拒绝构建EDT以外的UI类。我已经做了很多的Swing / AWT的,我知道的大部分关于EDT的规则。我用的SwingWorker,SwingUtilties.invokeLater修改组件。我总是尽管这部件可以 流构建的EDT之外,但必须 实现 操作在EDT。换句话说,你可以构建在后台,但收拾的呼叫设置默认/调用setVisible必须是美国东部时间以及任何后续调用操纵组件。

I've been integrating the Substance look and feel into my application and ran into several problems regarding it's internal EDT (Event Dispatch Thread) checking routines. Substance absolutely refuses to construct UI classes outside of the EDT. I've done plenty of Swing/AWT and I know most of the rules regarding the EDT. I use SwingWorker, SwingUtilties.invokeLater to modify components. I always though that components could be CONSTRUCTED outside of the EDT, but must be realized and manipulated on the EDT. In other words, you can construct and setup defaults in the background but the call to pack/setVisible must be EDT as well as any subsequent calls to manipulate the component.

我想问的原因是,我有一个特别结实窗口建设,涉及到许多小部件,状态和资源(大量的图标)。 previously,我构建了一个SwingWorker的背景方法的窗口,并取得了窗口done方法可见。从来没有一个单一的问题。当切换到物质,内部检查EDT咬我。

The reason I ask is that I have a particularly "beefy" window to construct, involving many widgets, state, and resources (lots of icons). Previously, I constructed the window on the background method of a SwingWorker and made the window visible in the done method. Never had a single problem. Upon switching to Substance, the internal EDT checking bites me.

我已经能够重构code来解决这个问题。我可以构建在EDT这是不是因为整个应用程序将阻止一个很好的解决方案。我也可以重构甚至更多,尽我所能来加载所有的额外资源的EDT之外。

I've been able to refactor code to get around this. I can construct on the EDT which isn't a good solution since the entire application will block. I can also refactor even more and try my best to load all of the extra resources outside of the EDT.

结束语起来......它是安全的构建的Swing / AWT部件没有在事件指派线程?

Wrapping it up ... Is it safe to construct Swing/AWT widgets NOT on the Event Dispatch Thread?

推荐答案

Sun公司已经改变了2004年规则 - 之前,你被允许创建EDT以外的组件和只有迁入EDT一旦有分量一直实现

Sun has changed the rules in 2004 -- before, you were allowed to create the components outside the EDT and only had to move into the EDT once the component had been realized.

新的规则现在读取:

要避免死锁的可能性,
  你必须采取格外小心秋千
  组件和模型的创建
  修改,并且只从被查询
  事件派发线程。

To avoid the possibility of deadlock, you must take extreme care that Swing components and models are created, modified, and queried only from the event-dispatching thread.

我这个博客文章提供了更多的细节,包括链接到其他相关的文章。注意,所有Sun官方<一个href=\"http://java.sun.com/docs/books/tutorial/uiswing/examples/start/HelloWorldSwingProject/src/start/HelloWorldSwing.java\">examples已经被改写,是非常严格的这个问题。

this blog post of mine gives more details, including links to other related articles. note that all official Sun examples have been rewritten and are very strict about this.

在历史上,它可能是多核心电脑作为台式机的激励规则的重新制定的日益普及 - 线程问题越来越明显的客户端堆栈,并正对EDT的准则非常严格,他们中的很多可以从一开始就pvented $ p $。

historically, it probably was the increasing availability of multi-core computers as desktop machines that motivated the re-formulation of the rule -- threading issues became more and more apparent on the client stack, and by being very strict on EDT guidelines, a lot of them can be prevented from the start.

这篇关于它是安全的构建在事件指派线程不可摆动/ AWT小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆