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?

dacwe
  • 43,066
  • 12
  • 116
  • 140
  • good question +1, my helicopter views :-) --> `a)` everything works until repaint() isn't locked by Thread.sleep(int), `b)` there were a few topics about isEventDispatchThread(), but those lins are lost on plundered Java.Net `c)` agreed with API for AWT Components and for their nested classes in Swing – mKorbel Mar 20 '12 at 12:26

3 Answers3

28

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.

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • Both links don't work anymore, and I cannot find the referenced text anywhere :( Also note that the methods are [not specified to be thread safe](https://stackoverflow.com/questions/20905985/is-the-swing-repaint-method-still-safe-to-use-outside-the-edt-in-java-7) even if they are... – Maarten Bodewes Nov 21 '20 at 14:59
  • 1
    As the links are obsolete [here](https://www.oracle.com/java/technologies/painting.html#paint_process) is an updated link. "JComponent.repaint() registers an asynchronous repaint request to the component's RepaintManager, which uses invokeLater() to queue a Runnable to later process the request on the event dispatching thread." – David Kroukamp Dec 21 '20 at 07:23
8

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.

Parker
  • 7,244
  • 12
  • 70
  • 92
mre
  • 43,520
  • 33
  • 120
  • 170
  • 1
    Thankx for this wonderful Painting Doc, I needed this stuff for so long :-), just couldn't find it. Seems like I was waiting for you to refer to it :-) – nIcE cOw Mar 20 '12 at 12:23
  • 2
    @GagandeepBali: I rely on it, too. One handy way to find it is through the [`Component`](http://docs.oracle.com/javase/7/docs/api/java/awt/Component.html) API. – trashgod Mar 20 '12 at 17:44
2

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

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    ehhh ........ ? The answer to the question is clearly YES - no need to muddy the waters by possibly misbehaving _unrelated_ code – kleopatra Mar 20 '12 at 13:15
  • I'll endorse mKorbel's healthy skepticism: 1) APIs can [change](http://stackoverflow.com/a/3245805/230513), although probably not `repaint()`. 2) While `repaint()` itself is thread-safe, one typically calls it after updating a component, which _isn't_ thread-safe. – trashgod Mar 20 '12 at 17:50
  • @trahgod thanks for the translation :-) Though I tend to disagree a bit: a) there was no change, but a fix of a doc error (arguably the same thingy, it was a long story) b) if so, the code _before_ calling repaint is wrong, not the repaint itself – kleopatra Mar 21 '12 at 09:01
  • "about the experience on this forum" is something for [meta], not for answers. – Maarten Bodewes Nov 21 '20 at 15:02