Safe to use Component.repaint() outside EDT?

送分小仙女□ 提交于 2019-11-27 05:46:25

问题


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.

Is this so? And where can I find some documentation/code?


回答1:


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(). The repaint() and revalidate() methods queue requests for the event-dispatching thread to call paint() and validate(), respectively.

EDIT 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.




回答2:


It is thread-safe. The RepaintManager ensures that such calls are placed in the Event Dispatch Thread.

Painting in AWT and Swing ("official" documentation)

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). 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"). Finally, it uses invokeLater() to process the pending requests on the event dispatching thread, as described in the section on "Repaint Processing" (option B).

For most programs, the RepaintManager can be viewed as part of Swing's internal system and can virtually be ignored. However, its API provides programs the option of gaining finer control over certain aspects of painting.




回答3:


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)

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(),

about Concurency in Swing are there notable numbers of questions



来源:https://stackoverflow.com/questions/9786497/safe-to-use-component-repaint-outside-edt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!