Java Reference
In-Depth Information
< Day Day Up >
12. Threads
This section concerns multithreaded programming. Multithreaded programming is difficult! The
overriding prescription is:
Avoid low-level multithreaded programming where possible.
Instead,
use higher level multithreaded abstractions, such as those introduced by
java.util.concurrent
in
release 5.0. This is an important special case of the advice in Trap 15.5.
12.1. Calling
Thread.run
doesn't start a thread
Prescription:
Never call
Thread.run
.
References:
Puzzle 76
.
12.2. Library classes may
lock
or
notify
their instances
Prescription:
Don't use an instance lock if you extend a library class. Instead, use a separate lock
object stored in a private field.
References:
Puzzle 77
; [EJ Item 15].
12.3.
Thread.interrupted
clears the interrupted status
Prescription:
Don't use
Thread.interrupted
unless you want to clear the interrupted status of the
current thread.
References:
Puzzle 84
.
12.4. Class initialization runs with the
Class
lock held
Prescription:
To avoid the risk of deadlock, never wait for a background thread during class
initialization.
References:
Puzzle 85
; [JLS 12.4.2].
12.5. Failure to synchronize when sharing mutable state can result in
failure to observe state changes
Prescription:
Synchronize access to shared mutable state.
References:
[EJ Item 48].
Search WWH ::
Custom Search