Top Description Inners Fields Constructors Methods
io.netty5.util.internal.shaded.org.jctools.maps

public Class NonBlockingHashMap<TypeK, TypeV>

extends AbstractMap<TypeK, TypeV>
implements ConcurrentMap<TypeK, TypeV>, Cloneable, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.lang.Cloneable, java.util.concurrent.ConcurrentMap, java.util.Map
Type Parameters
<TypeK>
the type of keys maintained by this map
<TypeV>
the type of mapped values
Imports
java.io.IOException, .Serializable, java.util.*, java.util.concurrent.ConcurrentMap, java.util.concurrent.atomic.AtomicLongFieldUpdater, .AtomicReferenceFieldUpdater, io.netty5.util.internal.shaded.org.jctools.util.RangeUtil

A lock-free alternate implementation of java.util.concurrent.ConcurrentHashMap with better scaling properties and generally lower costs to mutate the Map. It provides identical correctness properties as ConcurrentHashMap. All operations are non-blocking and multi-thread safe, including all update operations. NonBlockingHashMap scales substantially better than java.util.concurrent.ConcurrentHashMap for high update rates, even with a large concurrency factor. Scaling is linear up to 768 CPUs on a 768-CPU Azul box, even with 100% updates or 100% reads or any fraction in-between. Linear scaling up to all cpus has been observed on a 32-way Sun US2 box, 32-way Sun Niagara box, 8-way Intel box and a 4-way Power box. This class obeys the same functional specification as java.util.Hashtable, and includes versions of methods corresponding to each method of Hashtable. However, even though all operations are thread-safe, operations do not entail locking and there is not any support for locking the entire table in a way that prevents all access. This class is fully interoperable with Hashtable in programs that rely on its thread safety but not on its synchronization details.

Operations (including put) generally do not block, so may overlap with other update operations (including other puts and removes). Retrievals reflect the results of the most recently completed update operations holding upon their onset. For aggregate operations such as putAll, concurrent retrievals may reflect insertion or removal of only some entries. Similarly, Iterators and Enumerations return elements reflecting the state of the hash table at some point at or since the creation of the iterator/enumeration. They do not throw ConcurrentModificationException. However, iterators are designed to be used by only one thread at a time.

Very full tables, or tables with high re-probe rates may trigger an internal resize operation to move into a larger table. Resizing is not terribly expensive, but it is not free either; during resize operations table throughput may drop somewhat. All threads that visit the table during a resize will 'help' the resizing but will still be allowed to complete their operation before the resize is finished (i.e., a simple 'get' operation on a million-entry table undergoing resizing will not need to block until the entire million entries are copied).

This class and its views and iterators implement all of the optional methods of the Map and Iterator interfaces.

Like Hashtable but unlike HashMap, this class does not allow null to be used as a key or value.

Author
Cliff Click
Since
1.5

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
NonBlockingHashMap.CHM<TypeK, TypeV>

private class
private static class
private class
private class
private class

Field Summary

Modifier and TypeField and Description
private transient Object[]
private static final long
private transient long
private static final int
private static final int
private static final int
private transient ConcurrentAutoTable
pack-priv static volatile int
private static final Object
private static final int
private static final int
private static final Object
private static final int
private static final long
private static final NonBlockingHashMap.Prime
public static final Object

Constructor Summary

AccessConstructor and Description
public
NonBlockingHashMap()

Create a new NonBlockingHashMap with default minimum size (currently set to 8 K/V pairs or roughly 84 bytes on a standard 32-bit JVM).

public
NonBlockingHashMap(final int initial_sz)

Create a new NonBlockingHashMap with initial room for the given number of elements, thus avoiding internal resizing operations to reach an appropriate size.

Method Summary

Modifier and TypeMethod and Description
private static final boolean
CAS_key(Object[] kvs, int idx, Object old, Object key)

private final boolean
CAS_kvs(final Object[] oldkvs, final Object[] newkvs)

private static final boolean
CAS_val(Object[] kvs, int idx, Object old, Object val)

private static final NonBlockingHashMap.CHM<TypeK, TypeV>
chm(Object[] kvs)

public void
clear()

Overrides java.util.AbstractMap.clear.

Implements java.util.Map.clear.

Removes all of the mappings from this map.
public Object

Returns:

a clone of the hashtable.
clone
()

Overrides java.util.AbstractMap.clone.

Creates a shallow copy of this hashtable.
public boolean

Returns:

true if this map maps one or more keys to the specified value
contains
(Object
a value to search for
val
)

Legacy method testing if some key maps into the specified value in this table.

public boolean

Returns:

true if the key is in the table using the equals method
containsKey
(Object
key whose presence in this map is to be tested
key
)

Overrides java.util.AbstractMap.containsKey.

Implements java.util.Map.containsKey.

Tests if the key in the table using the equals method.
public boolean

Returns:

true if this map maps one or more keys to the specified value
containsValue
(final Object
value whose presence in this map is to be tested
val
)

Overrides java.util.AbstractMap.containsValue.

Implements java.util.Map.containsValue.

Returns true if this Map maps one or more keys to the specified value

Note

This method requires a full internal traversal of the hash table and is much slower than containsKey.

public Enumeration<TypeV>

Returns:

an enumeration of the values in this table
elements
()

Returns an enumeration of the values in this table.

public Set<Map.Entry<TypeK, TypeV>>
entrySet()

Implements abstract java.util.AbstractMap.entrySet.

Implements java.util.Map.entrySet.

Returns a Set view of the mappings contained in this map.
public TypeV
get(Object
the key whose associated value is to be returned
key
)

Overrides java.util.AbstractMap.get.

Implements java.util.Map.get.

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
private static final Object
get_impl(final NonBlockingHashMap<TypeK, TypeV> topmap, final Object[] kvs, final Object key)

public TypeK
getk(TypeK key)

Returns the Key to which the specified key is mapped, or null if this map contains no mapping for the key.

private static final Object
getk_impl(final NonBlockingHashMap<TypeK, TypeV> topmap, final Object[] kvs, final Object key)

private static final int
hash(final Object key)

private static final int[]
hashes(Object[] kvs)

private final Object[]
help_copy(Object[] helper)

private final void
initialize(int initial_sz)

protected final void
public boolean

Returns:

size() == 0
isEmpty
()

Overrides java.util.AbstractMap.isEmpty.

Implements java.util.Map.isEmpty.

Returns size() == 0.
private static final Object
key(Object[] kvs, int idx)

private static boolean
keyeq(Object K, Object key, int[] hashes, int hash, int fullhash)

public Enumeration<TypeK>

Returns:

an enumeration of the keys in this table
keys
()

Returns an enumeration of the keys in this table.

public Set<TypeK>
keySet()

Overrides java.util.AbstractMap.keySet.

Implements java.util.Map.keySet.

Returns a Set view of the keys contained in this map.
private static final int
len(Object[] kvs)

private static boolean
private final void
print2(Object[] kvs)

public TypeV

Returns:

the previous value associated with key, or null if there was no mapping for key
put
(TypeK
key with which the specified value is to be associated
key
,
TypeV
value to be associated with the specified key
val
)

Overrides java.util.AbstractMap.put.

Implements java.util.Map.put.

Maps the specified key to the specified value in the table.
public void
putAll(Map<? extends TypeK, ? extends TypeV>
mappings to be stored in this map
m
)

Overrides java.util.AbstractMap.putAll.

Implements java.util.Map.putAll.

Copies all of the mappings from the specified map to this one, replacing any existing mappings.
public TypeV

Returns:

the previous value associated with the specified key, or null if there was no mapping for the key
putIfAbsent
(TypeK
key with which the specified value is to be associated
key
,
TypeV
value to be associated with the specified key
val
)

Overrides default java.util.Map.putIfAbsent.

Implements java.util.concurrent.ConcurrentMap.putIfAbsent.

Atomically, do a put if-and-only-if the key is not mapped.
public final TypeV

Returns:

the previous value associated with the specified key, or null if there was no mapping for the key
putIfMatch
(Object key, Object newVal, Object oldVal)

Atomically replace newVal for oldVal, returning the value that existed there before.

private static final Object

Returns:

TOMBSTONE if key does not exist or match has failed. null if expVal is null AND old value was null. Otherwise the old entry value (not null).
putIfMatch0
(final NonBlockingHashMap<TypeK, TypeV>
the map to act on
topmap
,
final Object[]
the KV table snapshot we act on
kvs
,
final Object
not null (will result in NullPointerException)
key
,
final Object
the new value to use. Not null. TOMBSTONE will result in deleting the entry.
putval
,
final Object
expected old value. Can be null. NO_MATCH_OLD for an unconditional put/remove. TOMBSTONE if we expect old entry to not exist(null/TOMBSTONE value). MATCH_ANY will ignore the current value, but only if an entry exists. A null expVal is used internally to perform a strict insert-if-never-been-seen-before operation.
expVal
)

Put, Remove, PutIfAbsent, etc. Return the old value.

public final TypeV
putIfMatchAllowNull(Object key, Object newVal, Object oldVal)

public Object[]
private static long
rawIndex(final Object[] ary, final int idx)

private void
protected void
public TypeV

Returns:

the previous value associated with key, or null if there was no mapping for key
remove
(Object
key whose mapping is to be removed from the map
key
)

Overrides java.util.AbstractMap.remove.

Implements java.util.Map.remove.

Removes the key (and its corresponding value) from this map.
public boolean
remove(Object
key with which the specified value is associated
key
,
Object
value expected to be associated with the specified key
val
)

Overrides default java.util.Map.remove.

Implements java.util.concurrent.ConcurrentMap.remove.

Atomically do a remove(Object) if-and-only-if the key is mapped to a value which is equals to the given value.
public TypeV
replace(TypeK
key with which the specified value is associated
key
,
TypeV
value to be associated with the specified key
val
)

Overrides default java.util.Map.replace.

Implements java.util.concurrent.ConcurrentMap.replace.

Atomically do a put(key,val) if-and-only-if the key is mapped to some value already.
public boolean
replace(TypeK
key with which the specified value is associated
key
,
TypeV
value expected to be associated with the specified key
oldValue
,
TypeV
value to be associated with the specified key
newValue
)

Overrides default java.util.Map.replace.

Implements java.util.concurrent.ConcurrentMap.replace.

Atomically do a put(key,newValue) if-and-only-if the key is mapped a value which is equals to oldValue.
private static int
reprobe_limit(int len)

public long

Returns:

the count of reprobes since the last call to reprobes or since the table was created.
reprobes
()

Get and clear the current count of reprobes.

public int

Returns:

the number of key-value mappings in this map
size
()

Overrides java.util.AbstractMap.size.

Implements java.util.Map.size.

Returns the number of key-value mappings in this map.
public String

Returns:

a string representation of this map
toString
()

Overrides java.util.AbstractMap.toString.

Returns a string representation of this map.
private static final Object
val(Object[] kvs, int idx)

public Collection<TypeV>
values()

Overrides java.util.AbstractMap.values.

Implements java.util.Map.values.

Returns a Collection view of the values contained in this map.
private void
Inherited from java.util.AbstractMap:
equalshashCode