Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the readLock when creating an Iterator for the Map. #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
9 changes: 7 additions & 2 deletions src/main/java/net/jodah/expiringmap/ExpiringMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -714,8 +714,13 @@ public boolean contains(Object entry) {

@Override
public Iterator<Map.Entry<K, V>> iterator() {
return (entries instanceof EntryLinkedHashMap) ? ((EntryLinkedHashMap<K, V>) entries).new EntryIterator()
: ((EntryTreeHashMap<K, V>) entries).new EntryIterator();
readLock.lock();
try {
return (entries instanceof EntryLinkedHashMap) ? ((EntryLinkedHashMap<K, V>) entries).new EntryIterator()
: ((EntryTreeHashMap<K, V>) entries).new EntryIterator();
} finally {
readLock.unlock();
}
}

@Override
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/net/jodah/expiringmap/issues/Issue10.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
* Do not throw ConcurrentModificationException when using an Iterator
Expand Down Expand Up @@ -55,4 +61,34 @@ public void testKeySet() {
Assert.assertEquals((Integer) 3, iterator.next());
}
}

public void testParallelPutRemove() {
ExpiringMap<Integer, String> testee = ExpiringMap.create();
ExecutorService service = Executors.newFixedThreadPool(2);

Runnable adder = () -> {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
AtomicInteger counter = new AtomicInteger(0);
for (int j = 0; j < 5; j++) {
for (int i = 0; i < 100_000; i++) {
if (rnd.nextBoolean()) {
testee.put(counter.incrementAndGet(), "bar");
}
}
}
};

Runnable remover = () -> {
while (!service.isTerminated()) {
List<Integer> entriesToDelete = new ArrayList<>();
for (Map.Entry<Integer, String> e : testee.entrySet()) {
entriesToDelete.add(e.getKey());
}
entriesToDelete.forEach(testee.keySet()::remove);
}
};
service.submit(adder);
service.shutdown(); // schedule shutdown, let the running tasks finish
remover.run(); // run synchronous, waits for the adder threads to finish
}
}