Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8240162

ConcurrentHashMap can have values in it but returns 0 as size.

    Details

      Description

      ADDITIONAL SYSTEM INFORMATION :
      OS X but likely all.

      A DESCRIPTION OF THE PROBLEM :
      If you execute a lambda in computeIfPresent method that in turn calls computeIfPresent inside itself with a lambda that returns null and after completion returns null.

      Then try to put a value into the map the size will be zero.
      This is a bit messy description see "Steps to Reproduce" where I have added some code to reproduce this issue.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Class that reproduces this error and throws and exception if error is found.
      ====== ConcurrentHashMapTest.java ======

      import java.lang.IllegalStateException;
      import java.util.concurrent.ConcurrentHashMap;

      public class ConcurrentHashMapTest {

      public static void main(final String[] args)
      {
      final ConcurrentHashMap<String, String> concurrentHashMap = new ConcurrentHashMap<>();

      final String key = "key";
      final String value = "value";

      System.out.println("Put called with key=" + key + ", value=" + value + ".");
      concurrentHashMap.put(key, value);

      System.out.println("ConcurrentHashMap size=" + concurrentHashMap.size() + ".");

      System.out.println("Running nested computeIdPresent with null return");
      concurrentHashMap.computeIfPresent(key, (aKey, aValue) -> {
      concurrentHashMap.computeIfPresent(key, (bKey, bValue) -> {
      return null;
      });
      return null;
      });

      System.out.println("ConcurrentHashMap size=" + concurrentHashMap.size() + ".");

      System.out.println("Put called with key=" + key + ", value=" + value + ".");
      concurrentHashMap.put(key, value);

      final int mapSize = concurrentHashMap.size();
      final int expectedSize = 1;

      System.out.println("ConcurrentHashMap size=" + mapSize + ".");
      if (expectedSize != mapSize) {
      throw new IllegalStateException("Expected map to have size of value 1 after put operation.");
      }
      }
      }



      CUSTOMER SUBMITTED WORKAROUND :
      Refactor call to computeIfPresent lambda.

      FREQUENCY : always


        Attachments

          Activity

            People

            • Assignee:
              martin Martin Buchholz
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: