Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,35 @@ protected V defaultValue(K key) {
return null;
}

/**
* Removes the value corresponding to the already garbage collected {@link Reference}. This
* should generally only be used if overriding {@link #expungeStaleEntries()} to execute code
* when references have been collected.
*
* <pre>{@code
* class MyReferenceListener<K, V> extends WeakConcurrentMap.WithInlinedExpunction<K, V> {
* public void expungeStaleEntries() {
* Reference<?> reference;
* while ((reference = poll()) != null) {
* V value = expungeStaleEntry(reference);
* if (value != null) {
* reportOrphanedReference(value);
* }
* }
* }
* }</pre>
*/
protected final V expungeStaleEntry(Reference<?> reference) {
return target.remove(reference);
}

/**
* Cleans all unused references.
*/
public void expungeStaleEntries() {
Reference<?> reference;
while ((reference = poll()) != null) {
target.remove(reference);
expungeStaleEntry(reference);
}
}

Expand All @@ -189,7 +211,7 @@ public int approximateSize() {
public void run() {
try {
while (!Thread.interrupted()) {
target.remove(remove());
expungeStaleEntry(remove());
}
} catch (InterruptedException ignored) {
// do nothing
Expand Down Expand Up @@ -344,4 +366,4 @@ public V setValue(V value) {
return entry.setValue(value);
}
}
}
}