Swing Component Sync

I am reading Java Threads 3rd Ed. Oaks and Wong (O'Reilly, 2004). They carry an example of a Swing game throughout the book. The classes they define are mostly user subclasses of javax.swing.JComponent .

What seems completely wrong to me is that these JComponent threads are safe with various synchronization methods. I got the impression that Swing components should not be thread safe, but rather should always have access to the Swing event dispatch thread. (Interestingly, one of the few times they modify a component through a Swing EDT is for setText , which is one of the few Swing methods that do not need to be called from the EDT.)

I would like to learn from some of you who have a lot of experience writing / reading Swing code: How often do programmers synchronize Swing components rather than always changing them through EDT? Is this permissible?

EDIT:
I noticed that this is almost the same question as this thread . However, he does not say what programmers really do in the wild. I am puzzled that the O'Reilly book so openly violates the Threading Swing model.

EDIT:
I found that they briefly explain the Swing threading model somewhere in the middle of the book. Nevertheless, I would like to receive an answer to my question. I have a feeling that most readers of this book end up breaking the Swing threading model, as most of their examples do.

EDIT:
If you want to see the code, you can download the code examples as a zip file. See for example ch03 / example1 / AnimatedCharacterDisplayCanvas.

EDIT:
I just found out that setText will not be thread safe in Java7 (July 2011 release).

+6
source share
3 answers

It is trivial if synchronized methods are not executed on an EventQueue , they will not block the flow of sending events. Conversely, a method running in another thread should always use an EventQueue to send the code through invokeLater() , invokeAndWait() or a mechanism such as javax.swing.Timer or javax.swing.SwingWorker . As a practical matter, they are reliable. The examples may be correct, but they should be considered from this point of view.

EventQueue API says: "The only requirements are events ... are dispatched ... in the same order in which they are queued." In my opinion, this is tantamount to a β€œhappened before” relationship between java.util.concurrent and JLS. A more detailed discussion can be found here .

+2
source

You should never have synchronized blocks on Swing components, which will lead to strange problems when trying to render.

Swing is not thread safe because everything needs to be updated on the EDT, even when creating Swing components.

Long running processes must be ported to the background thread or SwingWorker. When a thread other than EDT needs to create components or make updates for a component, it must be wrapped using SwingUtilities.invokeLater ()

+2
source

"Swing components are not essentially thread safe, and usually after the Swing components are visible on the screen, you can only safely change your data from the event stream. If you change the data of the Swing component from any stream other than the event dispatch stream, you must take precautions to ensure data integrity, an exception to this rule is the setText method for the JTextComponent or any of its subclasses or any component of the Swing component whose documentation explicitly states that it is thread safe " Monica Paulan http://java.sun.com/developer/technicalArticles/Threads/swing/

-1
source

Source: https://habr.com/ru/post/891148/


All Articles