在Java 7+中,在EDT之外使用Swing repaint()方法是否仍然安全? [英] Is the Swing repaint() method still safe to use outside the EDT in Java 7+?

查看:163
本文介绍了在Java 7+中,在EDT之外使用Swing repaint()方法是否仍然安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道过去认为使用Swing的线程模型调用 repaint()和其他一些选定的方法是安全的,但我最近被告知在评论中并非如此。

I know that it used to be considered safe to call repaint() and a few other selected methods from any thread even with Swing's threading model, however I was recently told in a comment that this is not so.

谷歌发现很多旧的讨论说这是安全的,但最近没什么。所有以前说它安全的官方参考文献似乎已经消失了,我在各个论坛上发现了一些人讨论它是如何不再安全的。

Google found a lot of older discussion saying it is safe but nothing recently. All of the official references that used to say it is safe seem to have disappeared, and I found a few people in various forums discussing how it was no longer safe.

I找不到任何正式的东西来确认它是否存在 - 而且我真的希望看到一些解释改变逻辑的东西,如果它已被改变的话。考虑到破坏现有应用程序的风险有多么糟糕,删除它似乎是一个非常奇怪的功能。

I cannot find anything official to confirm whether it is or isn't though - and I'd really like to see something explaining the logic of the change if it has been changed. Considering how badly it would risk breaking existing applications it seems like a very odd feature to have removed.

我真的在寻找官方参考的链接(即Javadoc) ,oracle教程或源代码链接)说明这些方法是否可以安全地从任何线程调用。

Really I'm looking for a link to an official reference (i.e. Javadoc, oracle tutorial, or source code link) saying whether these methods are or are not still safe to call from any thread.

此处提及此问题:

安全使用在EDT之外的Component.repaint()?

从现在消失的Sun页面中给出一个引用说:

Gives a quote from a now disappeared Sun page saying:


以下JComponent方法可以安全地从任何线程调用:repaint(),revalidate()和invalidate()。 repaint()和revalidate()方法将事件派发线程的请求分别调用paint()和validate()。

The following JComponent methods are safe to call from any thread: repaint(), revalidate(), and invalidate(). The repaint() and revalidate() methods queue requests for the event-dispatching thread to call paint() and validate(), respectively.

这符合我的理解,但我现在找不到那个页面或任何类似的页面,我看到几个人的未经证实的谣言说它不再安全。但另一方面,我没有发现任何明确的说明此功能已经改变。

That matches to my understanding, but I cannot find that page or any similar page now and I have seen unconfirmed rumours from several people saying it is no longer safe. But on the other hand I can find nothing definitive saying that this feature has changed.

更改备注

可能有助于解决这个问题的是Oracle发布的关于Swing线程处理变化的官方声明。我找到了Java 7中的更改页面,但根本没有提到它,这些页面都没有提到任何方式的线程或EDT:

What may help solve this question is an official statement from Oracle about the changes in Swing thread handling. I found the "changes in Java 7" page but that didn't mention it at all, neither of these pages mention threading or the EDT in any way:

http://docs.oracle.com/javase/ 7 / docs / technotes / guides / swing / enhancements-7.html

http://docs.oracle.com/javase/7/docs/technotes/guides/awt/enhancements-7。 html

推荐答案

这是官方参考


Swing的线程策略



通常,Swing不是线程安全的。除非另有说明,否则必须在事件派发线程上访问所有Swing组件和相关类。

Swing's Threading Policy

In general Swing is not thread safe. All Swing components and related classes, unless otherwise documented, must be accessed on the event dispatching thread.

并且 repaint 方法不会另外记录。

And the repaint method does not "document otherwise".

要再次向你保证你不需要再看一个单独的方法的Javadoc了对于明确的答案,请参阅例如 Java 6 Javadoc

To doubly reassure you that you do not need to look any further than an individual method's Javadoc for the definitive answer, see for example how a method's thread safety was documented in Java 6 Javadoc.

显然,需要对规范性规范,描述性技术文章和任何具体实现的细节之间的区别进行更多澄清。 Javadoc所说的是:无法保证重绘是一种线程安全的方法。顺便提一下,Java 7中经常讨论的从大多数Swing API中删除线程安全标识的决定只是:合同的变更,而不是实现。

Apparently, more clarification is needed as to the distinction between normative specification, descriptive technical articles, and details of any specific implementation. What the Javadoc states is this: there is no guarantee that repaint is a thread-safe method. Incidentally, the often-discussed decision in Java 7 to remove the "thread-safe" designation from most of the Swing API was just that: a change in contract, not implementation.

OpenJDK 7中重绘的具体实现似乎是线程安全的,这与规范给出的保证无关。依赖于重绘或其他方法的线程安全性的代码已损坏,并且无法保证在所有Java实现上都能正常运行。

The specific implementation of repaint in OpenJDK 7 appears to be thread-safe, a fact which has nothing to do with guarantees given by the specification. Code which relies on the thread safety of repaint or other methods is broken and is not guaranteed to behave properly on all Java implementations.

这篇关于在Java 7+中,在EDT之外使用Swing repaint()方法是否仍然安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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