简体   繁体   English

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

[英]Safe to use Component.repaint() outside EDT?

I cannot find any official documentation saying that it is safe to call Component.repaint from another thread than the Event Dispatch Thread, the EDT. 我找不到任何官方文档说从除了事件调度线程(EDT)之外的另一个线程调用Component.repaint是安全的。

Is this so? 是这样吗? And where can I find some documentation/code? 我在哪里可以找到一些文档/代码?

Here is a quote from an official page stating that: 以下是官方页面引用

The following JComponent methods are safe to call from any thread: repaint() , revalidate() , and invalidate() . 以下JComponent方法可以安全地从任何线程调用: repaint()revalidate()invalidate() The repaint() and revalidate() methods queue requests for the event-dispatching thread to call paint() and validate() , respectively. repaint()revalidate()方法将事件派发线程的请求分别调用paint()validate()

EDIT 1 : 编辑1:


Since the previous link mentioned has been shifted. 由于前面提到的链接已经转移。 I am posting a new link , though it might take a bit more time to actually know the authenticity of this page, since it appears to be from Java though it originated from some University 's server, as can be seen from the address bar. 我发布了一个新的链接 ,虽然可能需要更多的时间才能真正了解这个页面的真实性,因为它似乎来自Java虽然它来自一些University的服务器,从地址栏可以看出。

It is thread-safe. 它是线程安全的。 The RepaintManager ensures that such calls are placed in the Event Dispatch Thread. RepaintManager确保将此类调用放在事件调度线程中。

Painting in AWT and Swing ("official" documentation) 在AWT和Swing中绘画(“官方”文档)

The purpose of Swing's RepaintManager class is to maximize the efficiency of repaint processing on a Swing containment hierarchy, and also to implement Swing's 'revalidation' mechanism (the latter will be a subject for a separate article). Swing的RepaintManager类的目的是最大化Swing包含层次结构上重绘处理的效率,并实现Swing的“重新验证”机制(后者将成为单独文章的主题)。 It implements the repaint mechanism by intercepting all repaint requests on Swing components (so they are no longer processed by the AWT) and maintaining its own state on what needs to be updated (known as "dirty regions"). 它通过拦截Swing组件上的所有重绘请求(因此它们不再由AWT处理)并在需要更新的内容上维护自己的状态(称为“脏区”)来实现重绘机制。 Finally, it uses invokeLater() to process the pending requests on the event dispatching thread, as described in the section on "Repaint Processing" (option B). 最后,它使用invokeLater()来处理事件调度线程上的挂起请求,如“重新绘制处理”一节(选项B)中所述。

For most programs, the RepaintManager can be viewed as part of Swing's internal system and can virtually be ignored. 对于大多数程序,RepaintManager可以被视为Swing内部系统的一部分,几乎可以忽略。 However, its API provides programs the option of gaining finer control over certain aspects of painting. 但是,它的API为程序提供了更好地控制绘画某些方面的选项。

about the experiences on this forum 关于这个论坛的经验

(+1 for both answerers) but, I think that not possible to answering your question correctly, part of methods Graphics(2D) required call for repaint() programatically, rest of them implementing this method (in API ) directly (sure some of them missing this method in API) (对于两个回答者来说都是+1)但是,我认为不可能正确回答你的问题,部分方法Graphics(2D)需要以编程方式调用repaint() ,其余的直接实现此方法(在API )(确保一些他们在API中遗漏了这个方法)

for part of Swing JComponents is maybe better to dis-agree, this forum is full of questions about Concurency in Swing , starting with Graphics(2D) thought JTextComponents , JTree , and ends (same way is declared as thread safe) with setText() , 对于Swing JComponents一部分可能更好的不同意,这个论坛充满了关于Concurency in Swing的问题,从Graphics(2D)思考JTextComponentsJTree ,并以setText()结束(同样的方式声明为线程安全setText()

about Concurency in Swing are there notable numbers of questions 关于Concurency in Swing有很多问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM