java.util.Map that performs well in in a highly
thread-contentious environment. The map supports very efficient
get, put,
remove and containsKey
operations, assuming (approximate) uniform hashing and
that the number of entries does not exceed the number of buckets. If the
number of entries exceeds the number of buckets or if the hash codes of the
objects are not uniformly distributed, these operations have a worst case
scenario that is proportional to the number of elements in the map
(O(n)).
Each bucket in the hash table has its own monitor, so two threads can
safely operate on the map at the same time, often without incurring any
monitor contention. This means that you don't have to wrap instances
of this class with java.;
instances are already thread-safe. Unfortunately, however, this means
that this map implementation behaves in ways you may find disconcerting.
Bulk operations, such as putAll or the
retainAll operation in collection
views, are not atomic. If two threads are simultaneously
executing
staticBucketMapInstance.putAll(map);and
staticBucketMapInstance.entrySet().removeAll(map.entrySet());then the results are generally random. Those two statement could cancel each other out, leaving
staticBucketMapInstance essentially
unchanged, or they could leave some random subset of map in
staticBucketMapInstance.
Also, much like an encyclopedia, the results of size() and
isEmpty() are out-of-date as soon as they are produced.
The iterators returned by the collection views of this class are not
fail-fast. They will never raise a
java.. Keys and values
added to the map after the iterator is created do not necessarily appear
during iteration. Similarly, the iterator does not necessarily fail to
return keys and values that were removed after the iterator was created.
Finally, unlike java.-style implementations, this
class never rehashes the map. The number of buckets is fixed
at construction time and never altered. Performance may degrade if
you do not allocate enough buckets upfront.
The atomic(Runnable) method is provided to allow atomic iterations
and bulk operations; however, overuse of atomic
will basically result in a map that's slower than an ordinary synchronized
java..
Use this class if you do not require reliable bulk operations and
iterations, or if you can make your own guarantees about how bulk
operations will affect the map.
| Modifier and Type | Class and Description |
|---|---|
| private class | |
| private class | |
| private class | |
| private class | |
| private class | |
| private static class | StaticBucketMap.Lock
The lock object, which also includes a count of the nodes in this lock. |
| private static class | |
| private class | |
| private class |
| Modifier and Type | Field and Description |
|---|---|
| private final StaticBucketMap. | buckets
The array of buckets, where the actual data is held |
| private static final int | DEFAULT_BUCKETS
The default number of buckets to use |
| private final StaticBucketMap. | locks
The matching array of locks |
| Access | Constructor and Description |
|---|---|
| public | |
| public | StaticBucketMap(final int
the number of buckets for this map numBuckets)Initializes the map with a specified number of buckets. |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| private void | |
| public void | clear()
Implements java. |
| public boolean | Returns: true if foundthe key to check key)Implements java. |
| public boolean | Returns: true if foundthe value to check value)Implements java. |
| public Set | Returns: the entry setImplements java. |
| public boolean | Returns: true if equalthe object to compare to obj)Overrides java. Implements java. |
| public V | Returns: the associated valuethe key to retrieve key)Implements java. |
| private int | |
| public int | Returns: the hash codeOverrides java. Implements java. |
| public boolean | Returns: true if emptyImplements java. |
| public Set | Returns: the key setImplements java. |
| public V | Returns: the previous mapping for the keythe key to use key, final V the value to use value)Implements java. |
| public void | putAll(final Map<? extends K, ? extends V>
the map of entries to add map)Implements java. |
| public V | Returns: the previous value at this keythe key to remove key)Implements java. |
| public int | Returns: the current sizeImplements java. |
| public Collection | Returns: the valuesImplements java. |