Top Description Inners Fields Constructors Methods
io.opentelemetry.context.internal.shaded

public Class WeakConcurrentMap<K, V>

extends AbstractWeakConcurrentMap<K, V, WeakConcurrentMap.LookupKey<K>>
Class Inheritance
Known Direct Subclasses
io.opentelemetry.context.internal.shaded.WeakConcurrentMap.WithInlinedExpunction, io.opentelemetry.context.StrictContextStorage.PendingScopes
Annotations
@SuppressWarnings:HashCodeToString, MissingSummary, UngroupedOverloads, ThreadPriorityCheck, FieldMissingNullable
Imports
java.util.Iterator, .Map, java.util.concurrent.ConcurrentHashMap, .ConcurrentMap, java.util.concurrent.atomic.AtomicLong

A thread-safe map with weak keys. Entries are based on a key's system hash code and keys are considered equal only by reference equality. This class does not implement the java.util.Map interface because this implementation is incompatible with the map contract. While iterating over a map's entries, any key that has not passed iteration is referenced non-weakly.

This class has been copied as is from https://github.com/raphw/weak-lock-free/blob/ad0e5e0c04d4a31f9485bf12b89afbc9d75473b3/src/main/java/com/blogspot/mydailyjava/weaklockfree/WeakConcurrentMap.java

This class is internal and is hence not for public use. Its APIs are unstable and can change at any time.

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
public static class
WeakConcurrentMap.WithInlinedExpunction<K, V>

A WeakConcurrentMap where stale entries are removed as a side effect of interacting with this map.

Field Summary

Modifier and TypeField and Description
private static final AtomicLong
private static final ThreadLocal<WeakConcurrentMap.LookupKey<?>>
LOOKUP_KEY_CACHE

Lookup keys are cached thread-locally to avoid allocations on lookups.

private final boolean
private final Thread
Inherited from io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap:
target

Constructor Summary

AccessConstructor and Description
public
WeakConcurrentMap(boolean
true if a thread should be started that removes stale entries.
cleanerThread
)

public
WeakConcurrentMap(boolean
true if a thread should be started that removes stale entries.
cleanerThread
,
boolean
true if the lookup keys should be reused via a ThreadLocal. Note that setting this to true may result in class loader leaks. See isPersistentClassLoader(ClassLoader) for more details.
reuseKeys
)

public
WeakConcurrentMap(boolean
true if a thread should be started that removes stale entries.
cleanerThread
,
boolean
true if the lookup keys should be reused via a ThreadLocal. Note that setting this to true may result in class loader leaks. See isPersistentClassLoader(ClassLoader) for more details.
reuseKeys
,
ConcurrentMap<AbstractWeakConcurrentMap.WeakKey<K>, V>
ConcurrentMap implementation that this class wraps.
target
)

Method Summary

Modifier and TypeMethod and Description
public Thread

Returns:

The cleaner thread or null if no such thread was set.
getCleanerThread
()

protected WeakConcurrentMap.LookupKey<K>
getLookupKey(K key)

Implements abstract io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap.getLookupKey.

Override with care as it can cause lookup failures if done incorrectly.

private static boolean

Returns:

true if the provided class loader can be unloaded.
isPersistentClassLoader
(ClassLoader
The class loader to check.
classLoader
)

Checks whether the provided ClassLoader may be unloaded like a web application class loader, for example.

protected void
Inherited from io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap:
approximateSizeclearcontainsKeydefaultValueexpungeStaleEntriesgetgetIfPresentiteratorputputIfAbsentputIfProbablyAbsentremoveruntoString

Field Detail

IDback to summary
private static final AtomicLong ID
LOOKUP_KEY_CACHEback to summary
private static final ThreadLocal<WeakConcurrentMap.LookupKey<?>> LOOKUP_KEY_CACHE

Lookup keys are cached thread-locally to avoid allocations on lookups. This is beneficial as the JIT unfortunately can't reliably replace the LookupKey allocation with stack allocations, even though the LookupKey does not escape.

reuseKeysback to summary
private final boolean reuseKeys
threadback to summary
private final Thread thread

Constructor Detail

WeakConcurrentMapback to summary
public WeakConcurrentMap(boolean cleanerThread)
Parameters
cleanerThread:boolean

true if a thread should be started that removes stale entries.

WeakConcurrentMapback to summary
public WeakConcurrentMap(boolean cleanerThread, boolean reuseKeys)
Parameters
cleanerThread:boolean

true if a thread should be started that removes stale entries.

reuseKeys:boolean

true if the lookup keys should be reused via a ThreadLocal. Note that setting this to true may result in class loader leaks. See isPersistentClassLoader(ClassLoader) for more details.

WeakConcurrentMapback to summary
public WeakConcurrentMap(boolean cleanerThread, boolean reuseKeys, ConcurrentMap<AbstractWeakConcurrentMap.WeakKey<K>, V> target)
Parameters
cleanerThread:boolean

true if a thread should be started that removes stale entries.

reuseKeys:boolean

true if the lookup keys should be reused via a ThreadLocal. Note that setting this to true may result in class loader leaks. See isPersistentClassLoader(ClassLoader) for more details.

target:ConcurrentMap<AbstractWeakConcurrentMap.WeakKey<K>, V>

ConcurrentMap implementation that this class wraps.

Method Detail

getCleanerThreadback to summary
public Thread getCleanerThread()
Returns:Thread

The cleaner thread or null if no such thread was set.

getLookupKeyback to summary
protected WeakConcurrentMap.LookupKey<K> getLookupKey(K key)

Implements abstract io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap.getLookupKey.

Doc from io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap.getLookupKey.

Override with care as it can cause lookup failures if done incorrectly. The result must have the same Object#hashCode() as the input and be equal to a weak reference of the key. When overriding this, also override resetLookupKey.

Annotations
@Override
@SuppressWarnings:unchecked
isPersistentClassLoaderback to summary
private static boolean isPersistentClassLoader(ClassLoader classLoader)

Checks whether the provided ClassLoader may be unloaded like a web application class loader, for example.

If the class loader can't be unloaded, it is safe to use ThreadLocals and to reuse the LookupKey. Otherwise, the use of ThreadLocals may lead to class loader leaks as it prevents the class loader this class is loaded by to unload.

Parameters
classLoader:ClassLoader

The class loader to check.

Returns:boolean

true if the provided class loader can be unloaded.

resetLookupKeyback to summary
protected void resetLookupKey(WeakConcurrentMap.LookupKey<K> lookupKey)

Implements abstract io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap.resetLookupKey.

Doc from io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap.resetLookupKey.

Resets any reusable state in the lookup key.

Annotations
@Override
io.opentelemetry.context.internal.shaded back to summary

pack-priv final Class WeakConcurrentMap.LookupKey<K>

extends Object
Class Inheritance
  • java.lang.Object
  • io.opentelemetry.context.internal.shaded.WeakConcurrentMap.LookupKey

Field Summary

Modifier and TypeField and Description
private int
private K

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

pack-priv void
reset()

Failing to reset a lookup key can lead to memory leaks as the key is strongly referenced.

pack-priv WeakConcurrentMap.LookupKey<K>
withValue(K key)

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

hashCodeback to summary
private int hashCode
keyback to summary
private K key

Constructor Detail

LookupKeyback to summary
pack-priv LookupKey()

Method Detail

equalsback to summary
public boolean equals(Object other)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
other:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
resetback to summary
pack-priv void reset()

Failing to reset a lookup key can lead to memory leaks as the key is strongly referenced.

withValueback to summary
pack-priv WeakConcurrentMap.LookupKey<K> withValue(K key)
io.opentelemetry.context.internal.shaded back to summary

public Class WeakConcurrentMap.WithInlinedExpunction<K, V>

extends WeakConcurrentMap<K, V>
Class Inheritance

A WeakConcurrentMap where stale entries are removed as a side effect of interacting with this map.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public int
approximateSize()

Overrides io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap.approximateSize.

Returns the approximate size of this map where the returned number is at least as big as the actual number of entries.

public boolean
public V
public Iterator<Map.Entry<K, V>>
public V
put(K
The key of the entry.
key
,
V
The value of the entry.
value
)

Overrides io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap.put.

public V
Inherited from io.opentelemetry.context.internal.shaded.WeakConcurrentMap:
getCleanerThreadgetLookupKeyresetLookupKey

Constructor Detail

WithInlinedExpunctionback to summary
public WithInlinedExpunction()

Method Detail

approximateSizeback to summary
public int approximateSize()

Overrides io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap.approximateSize.

Doc from io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap.approximateSize.

Returns the approximate size of this map where the returned number is at least as big as the actual number of entries.

Returns:int

The minimum size of this map.

Annotations
@Override
containsKeyback to summary
public boolean containsKey(K key)

Overrides io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap.containsKey.

Parameters
key:K

Doc from io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap.containsKey.

The key of the entry.

Returns:boolean

Doc from io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap.containsKey.

true if the key already defines a value.

Annotations
@Override
getback to summary
public V get(K key)

Overrides io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap.get.

Parameters
key:K

Doc from io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap.get.

The key of the entry.

Returns:V

Doc from io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap.get.

The value of the entry or the default value if it did not exist.

Annotations
@Override
iteratorback to summary
public Iterator<Map.Entry<K, V>> iterator()

Overrides io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap.iterator.

Implements java.lang.Iterable.iterator.

Doc from java.lang.Iterable.iterator.

Returns an iterator over elements of type T.

Returns:Iterator<Map.Entry<K, V>>

an Iterator.

Annotations
@Override
putback to summary
public V put(K key, V value)

Overrides io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap.put.

Parameters
key:K

Doc from io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap.put.

The key of the entry.

value:V

Doc from io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap.put.

The value of the entry.

Returns:V

Doc from io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap.put.

The previous entry or null if it does not exist.

Annotations
@Override
removeback to summary
public V remove(K key)

Overrides io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap.remove.

Parameters
key:K

Doc from io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap.remove.

The key of the entry.

Returns:V

Doc from io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap.remove.

The removed entry or null if it does not exist.

Annotations
@Override