com.blogspot.mydailyjava.weaklockfree.WeakConcurrentMap Maven / Gradle / Ivy
package com.blogspot.mydailyjava.weaklockfree;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import 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 {@link 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.
*/
public class WeakConcurrentMap extends AbstractWeakConcurrentMap> {
/**
* Lookup keys are cached thread-locally to avoid allocations on lookups.
* This is beneficial as the JIT unfortunately can't reliably replace the {@link LookupKey} allocation
* with stack allocations, even though the {@link LookupKey} does not escape.
*/
private static final ThreadLocal> LOOKUP_KEY_CACHE = new ThreadLocal>() {
@Override
protected LookupKey> initialValue() {
return new LookupKey