简体   繁体   中英

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.

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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