Skip to content

NonBlockingHashMap remove bug #205

@fvlad

Description

@fvlad

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions