Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking Swing components synchronized
    primarykey
    data
    text
    <p>I'm reading <em>Java Threads</em> 3rd Ed. by Oaks and Wong (O'Reilly 2004). They carry an example of a Swing typing game throughout the book. The classes they define are mostly custom subclasses of <code>javax.swing.JComponent</code>.</p> <p>What seems quite wrong to me is that they make those <code>JComponent</code>s thread safe with various synchronization methods. I was under the impression that Swing components should not be thread safe, but rather that they should always be accessed from the Swing event dispatching thread. (Amusingly, one of the few times where they modify a component through the Swing EDT, it's for a <code>setText</code>, which is one of the very few Swing methods that do not need to be called from the EDT.)</p> <p>I would like to know from some of you who have a lot of experience writing/reading Swing code: Is it common for programmers to make Swing components synchronized instead of always modifying them through the EDT? Is it tolerable?</p> <p><strong>EDIT:</strong><br> I noticed it is nearly the same question as this <a href="https://stackoverflow.com/questions/3643174/is-update-from-edt-in-swing-an-absolute-rule-or-are-there-exceptions">thread</a>. However it does not say what programmers actually do in the wild. I'm puzzled that an O'Reilly book would so blatantly violate the Swing threading model.</p> <p><strong>EDIT:</strong><br> I discovered that they do briefly explain somewhere in the middle of the book the Swing threading model. Nonetheless I'd like to have an answer to my question. I have the feeling most who read this book will end up violating the Swing threading model since most of their examples do.</p> <p><strong>EDIT:</strong><br> If you want to look at the code, you can <a href="http://oreilly.com/catalog/9780596007829/" rel="nofollow noreferrer">Download examples code</a> as a zip file. See for example ch03/example1/AnimatedCharacterDisplayCanvas.</p> <p><strong>EDIT:</strong><br> I just learned that <code>setText</code> will not be thread-safe in Java7 (release in July 2011).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload