A customized implementation of
to operate in a multithreaded environment where the large majority of
method calls are read-only, instead of structural changes. When operating
in "fast" mode, read calls are non-synchronized and write calls perform the
- Clone the existing collection
- Perform the modification on the clone
- Replace the existing collection with the (modified) clone
When first created, objects of this class default to "slow" mode, where
all accesses of any type are synchronized but no cloning takes place. This
is appropriate for initially populating the collection, followed by a switch
to "fast" mode (by calling
setFast(true)) after initialization
NOTE: If you are creating and accessing a
TreeMap only within a single thread, you should use
java.util.TreeMap directly (with no synchronization), for
NOTE: This class is not cross-platform.
Using it may cause unexpected failures on some architectures.
It suffers from the same problems as the double-checked locking idiom.
In particular, the instruction that clones the internal collection and the
instruction that sets the internal reference to the clone can be executed
or perceived out-of-order. This means that any read operation might fail
unexpectedly, as it may be reading the state of the internal collection
before the internal collection is fully formed.
For more information on the double-checked locking idiom, see the
Double-Checked Locking Idiom Is Broken Declaration.