-
Notifications
You must be signed in to change notification settings - Fork 586
NonBlockingHashMap remove bug #205
Copy link
Copy link
Closed
Labels
Description
Hi. I was playing with NonBlockingHashMap and found that removes can return one items more than once when executed concurrently. Here is the test sample:
@Test
public void testMap() throws Exception {
final ExecutorService executor = Executors.newFixedThreadPool(32);
final ConcurrentMap<String, String> map = new NonBlockingHashMap<>();
final String key = UUID.randomUUID().toString();
final int numberOfIterations = 10_000;
final CountDownLatch latch = new CountDownLatch(numberOfIterations);
CopyOnWriteArrayList<String> addedList = new CopyOnWriteArrayList<>();
CopyOnWriteArrayList<String> removedList = new CopyOnWriteArrayList<>();
for (int i = 0; i < numberOfIterations; i++) {
if (i % 2 == 0) {
executor.submit(() -> {
String added = UUID.randomUUID().toString();
map.put(key, added);
addedList.add(added);
latch.countDown();
});
} else {
executor.submit(() -> {
String removed = map.remove(key);
if (removed != null) {
removedList.add(removed);
}
latch.countDown();
});
}
}
assertTrue(latch.await(5, TimeUnit.SECONDS));
assertEquals(addedList.size(), Sets.newHashSet(addedList).size());
assertEquals(removedList.size(), Sets.newHashSet(removedList).size());
}
My last assertEquals fails with number of actual removed items more than unique items.
Reactions are currently unavailable