[main] Fix obvious race condition in weak collections

This commit is contained in:
Johannes Frohnmeyer 2022-06-17 12:21:07 +02:00
parent eb71fb6533
commit b8d496f716
Signed by: Johannes
GPG Key ID: E76429612C2929F4
2 changed files with 12 additions and 8 deletions

View File

@ -18,9 +18,11 @@ public class WeakSet<E> extends AbstractSet<E> {
@Override
public boolean hasNext() {
if (delegate.hasNext()) {
next = unwrap(delegate.next());
return true;
synchronized (WeakSet.this) {
if (delegate.hasNext()) {
next = unwrap(delegate.next());
return true;
}
}
return false;
}
@ -66,7 +68,7 @@ public class WeakSet<E> extends AbstractSet<E> {
}
@Override
public void clear() {
public synchronized void clear() {
processQueue();
delegate.clear();
}

View File

@ -187,10 +187,12 @@ public class WeakValueMap<K, V> implements Map<K, V> {
@Override
public boolean hasNext() {
if (delegate.hasNext()) {
Entry<K, WeakValue<K, V>> ent = delegate.next();
next = new DelegateEntry(ent, unwrap(ent.getValue()));
return true;
synchronized (WeakValueMap.this) {
if (delegate.hasNext()) {
Entry<K, WeakValue<K, V>> ent = delegate.next();
next = new DelegateEntry(ent, unwrap(ent.getValue()));
return true;
}
}
return false;
}