Is the Swing repaint() method still safe to use outside the EDT in Java 7+?

后端 未结 3 1750
北恋
北恋 2020-11-29 10:12

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 r

3条回答
  •  攒了一身酷
    2020-11-29 10:57

    As discussed in Painting in AWT and Swing: Paint Processing,

    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.

    This is a necessary, but not sufficient, condition to establish a happens-before relation between successive calls to repaint(). As a practical matter, you still need to synchronize access to any data that is shared between threads. Without this, there's no way to ensure the visibility of any changes meant to influence the subsequent call to repaint().

提交回复
热议问题