Java Reference
In-Depth Information
NumberRange is not thread-safe; it does not preserve the invariant that constrains lower
and upper . The setLower and setUpper methods attempt to respect this invariant, but
do so poorly. Both setLower and setUpper are check-then-act sequences, but they do
not use sufficient locking to make them atomic. If the number range holds (0, 10), and one
thread calls setLower(5) while another thread calls setUpper(4) , with some unlucky
timing both will pass the checks in the setters and both modifications will be applied. The res-
ult is that the range now holds (5, 4)—an invalid state. So while the underlying AtomicIn-
teger s are thread-safe, the composite class is not. Because the underlying state variables
lower and upper are not independent, NumberRange cannot simply delegate thread
safety to its thread-safe state varaibles.
Search WWH ::




Custom Search