Java Reference
In-Depth Information
state set by the last code that happened to use the same thread, in-
stead of an uninitialized state expected at the beginning of a new thread
of execution. If you write a general class that uses
ThreadLocal
objects
and someone uses your class in a thread pool, the behavior might be
dangerously wrong.
For example, the
users
variable in the
Operations
example above could
have just this problemif an
Operations
object was used in multiple
threads, or different
Operations
objects were used in the same thread,
the "current user" notion could easily be wrong. Programmers who use
Operations
objects must understand this and only use the objects in
threading situations that match the assumptions of the class's code.