7.5 Summary
The Swing classes comprise one of the largest set of classes in the
Java API. While threads are an integral part of Java, the Swing
classes themselves are not threadsafe. This places a responsibility
on the developer, who must make sure that she follows the appropriate
access patterns for Swing classes. Methods on Swing objects (with a
few exceptions) can be invoked only on the event-dispatching thread.
Swing's use of the invokeLater()
method gives us a hint about how we might handle thread-unsafe
libraries in general: as long as access to those libraries occurs
only on a single thread, we will not run into any threading problems.
Passing a Runnable object to a thread pool that
contains a single thread is precisely analogous to the technique used
by the Swing classes.
7.5.1 Example Classes
Here are the class names and Ant targets for the examples in this
chapter:
|
Description
|
Main Java class
|
Ant target
|
|---|
|
Swing Type Tester (all components threadsafe)
|
javathreads.examples.ch07.example1.SwingTypeTester
|
ch7-ex1
| |
Swing Type Tester (uses invokeAndWait)
|
javathreads.examples.ch07.example2.SwingTypeTester
|
ch7-ex2
| |
Swing Type Tester with simulated server connection
|
javathreads.examples.ch07.example3.SwingTypeTester
|
ch7-ex3
|
|