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).
toto2 source share