Class WeakConcurrentMap<K,V>
- java.lang.Object
-
- java.lang.ref.ReferenceQueue<K>
-
- io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap<K,V,WeakConcurrentMap.LookupKey<K>>
-
- io.opentelemetry.context.internal.shaded.WeakConcurrentMap<K,V>
-
- All Implemented Interfaces:
java.lang.Iterable<java.util.Map.Entry<K,V>>
,java.lang.Runnable
- Direct Known Subclasses:
StrictContextStorage.PendingScopes
,WeakConcurrentMap.WithInlinedExpunction
public class WeakConcurrentMap<K,V> extends AbstractWeakConcurrentMap<K,V,WeakConcurrentMap.LookupKey<K>>
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 theMap
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 Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
WeakConcurrentMap.LookupKey<K>
static class
WeakConcurrentMap.WithInlinedExpunction<K,V>
AWeakConcurrentMap
where stale entries are removed as a side effect of interacting with this map.-
Nested classes/interfaces inherited from class io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap
AbstractWeakConcurrentMap.WeakKey<K>
-
-
Field Summary
Fields Modifier and Type Field Description private static java.util.concurrent.atomic.AtomicLong
ID
private static java.lang.ThreadLocal<WeakConcurrentMap.LookupKey<?>>
LOOKUP_KEY_CACHE
Lookup keys are cached thread-locally to avoid allocations on lookups.private boolean
reuseKeys
private java.lang.Thread
thread
-
Fields inherited from class io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap
target
-
-
Constructor Summary
Constructors Constructor Description WeakConcurrentMap(boolean cleanerThread)
WeakConcurrentMap(boolean cleanerThread, boolean reuseKeys)
WeakConcurrentMap(boolean cleanerThread, boolean reuseKeys, java.util.concurrent.ConcurrentMap<AbstractWeakConcurrentMap.WeakKey<K>,V> target)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Thread
getCleanerThread()
protected WeakConcurrentMap.LookupKey<K>
getLookupKey(K key)
Override with care as it can cause lookup failures if done incorrectly.private static boolean
isPersistentClassLoader(java.lang.ClassLoader classLoader)
Checks whether the providedClassLoader
may be unloaded like a web application class loader, for example.protected void
resetLookupKey(WeakConcurrentMap.LookupKey<K> lookupKey)
Resets any reusable state in the lookup key.-
Methods inherited from class io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap
approximateSize, clear, containsKey, defaultValue, expungeStaleEntries, get, getIfPresent, iterator, put, putIfAbsent, putIfProbablyAbsent, remove, run, toString
-
-
-
-
Field Detail
-
LOOKUP_KEY_CACHE
private static final java.lang.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 theWeakConcurrentMap.LookupKey
allocation with stack allocations, even though theWeakConcurrentMap.LookupKey
does not escape.
-
ID
private static final java.util.concurrent.atomic.AtomicLong ID
-
thread
private final java.lang.Thread thread
-
reuseKeys
private final boolean reuseKeys
-
-
Constructor Detail
-
WeakConcurrentMap
public WeakConcurrentMap(boolean cleanerThread)
- Parameters:
cleanerThread
-true
if a thread should be started that removes stale entries.
-
WeakConcurrentMap
public WeakConcurrentMap(boolean cleanerThread, boolean reuseKeys)
- Parameters:
cleanerThread
-true
if a thread should be started that removes stale entries.reuseKeys
-true
if the lookup keys should be reused via aThreadLocal
. Note that setting this totrue
may result in class loader leaks. SeeisPersistentClassLoader(ClassLoader)
for more details.
-
WeakConcurrentMap
public WeakConcurrentMap(boolean cleanerThread, boolean reuseKeys, java.util.concurrent.ConcurrentMap<AbstractWeakConcurrentMap.WeakKey<K>,V> target)
- Parameters:
cleanerThread
-true
if a thread should be started that removes stale entries.reuseKeys
-true
if the lookup keys should be reused via aThreadLocal
. Note that setting this totrue
may result in class loader leaks. SeeisPersistentClassLoader(ClassLoader)
for more details.target
- ConcurrentMap implementation that this class wraps.
-
-
Method Detail
-
isPersistentClassLoader
private static boolean isPersistentClassLoader(java.lang.ClassLoader classLoader)
Checks whether the providedClassLoader
may be unloaded like a web application class loader, for example.If the class loader can't be unloaded, it is safe to use
ThreadLocal
s and to reuse theWeakConcurrentMap.LookupKey
. Otherwise, the use ofThreadLocal
s may lead to class loader leaks as it prevents the class loader this class is loaded by to unload.- Parameters:
classLoader
- The class loader to check.- Returns:
true
if the provided class loader can be unloaded.
-
getLookupKey
protected WeakConcurrentMap.LookupKey<K> getLookupKey(K key)
Description copied from class:AbstractWeakConcurrentMap
Override with care as it can cause lookup failures if done incorrectly. The result must have the sameObject.hashCode()
as the input and beequal to
a weak reference of the key. When overriding this, also overrideAbstractWeakConcurrentMap.resetLookupKey(L)
.- Specified by:
getLookupKey
in classAbstractWeakConcurrentMap<K,V,WeakConcurrentMap.LookupKey<K>>
-
resetLookupKey
protected void resetLookupKey(WeakConcurrentMap.LookupKey<K> lookupKey)
Description copied from class:AbstractWeakConcurrentMap
Resets any reusable state in the lookup key.- Specified by:
resetLookupKey
in classAbstractWeakConcurrentMap<K,V,WeakConcurrentMap.LookupKey<K>>
-
getCleanerThread
public java.lang.Thread getCleanerThread()
- Returns:
- The cleaner thread or
null
if no such thread was set.
-
-