Java Reference
In-Depth Information
If you are tempted to register an event listener or start a thread from a constructor, you can
avoid the improper construction by using a private constructor and a public factory method,
as shown in SafeListener in Listing 3.8 .
Listing 3.8. Using a Factory Method to Prevent the this Reference from Escaping During
Construction.
3.3. Thread Confinement
Accessing shared, mutable data requires using synchronization; one way to avoid this re-
quirement is to not share . If data is only accessed from a single thread, no synchronization
is needed. This technique, thread confinement , is one of the simplest ways to achieve thread
safety. When an object is confined to a thread, such usage is automatically thread-safe even
if the confined object itself is not [CPJ 2.3.2].
Swing uses thread confinement extensively. The Swing visual components and data model
objects are not thread safe; instead, safety is achieved by confining them to the Swing event
dispatch thread. To use Swing properly, code running in threads other than the event thread
Search WWH ::




Custom Search