Is ConcurrentHashMap's computeIfAbsent thread safe, when nested?











up vote
1
down vote

favorite












I am new to using Map's computeIfAbsent method. I did find (per Java docs) that the entire method invocation is performed atomically. I would like to know (rather confirm) if the following code snippet (inside the method) would be executed atomically.



     ConcurrentMap<RunMode, Map<LocalDate, Map<Integer, Set<DomainObject>>>> myCache = new ConcurrentHashmap<>();


public void addToCache (RunMode runMode, LocalDate bizDate, DomainObject bean) {



    Set<DomainObject> domainObjSet = myCache.computeIfAbsent(runMode, runModeMap-> new ConcurrentHashMap<>())                
.computeIfAbsent(bizDate, bizDateMap-> new ConcurrentHashMap<>())
.computeIfAbsent(bean.getId(), domainSet-> Collections.synchronizedSet(new HashSet<>()).add(bean));

}









share|improve this question


























    up vote
    1
    down vote

    favorite












    I am new to using Map's computeIfAbsent method. I did find (per Java docs) that the entire method invocation is performed atomically. I would like to know (rather confirm) if the following code snippet (inside the method) would be executed atomically.



         ConcurrentMap<RunMode, Map<LocalDate, Map<Integer, Set<DomainObject>>>> myCache = new ConcurrentHashmap<>();


    public void addToCache (RunMode runMode, LocalDate bizDate, DomainObject bean) {



        Set<DomainObject> domainObjSet = myCache.computeIfAbsent(runMode, runModeMap-> new ConcurrentHashMap<>())                
    .computeIfAbsent(bizDate, bizDateMap-> new ConcurrentHashMap<>())
    .computeIfAbsent(bean.getId(), domainSet-> Collections.synchronizedSet(new HashSet<>()).add(bean));

    }









    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am new to using Map's computeIfAbsent method. I did find (per Java docs) that the entire method invocation is performed atomically. I would like to know (rather confirm) if the following code snippet (inside the method) would be executed atomically.



           ConcurrentMap<RunMode, Map<LocalDate, Map<Integer, Set<DomainObject>>>> myCache = new ConcurrentHashmap<>();


      public void addToCache (RunMode runMode, LocalDate bizDate, DomainObject bean) {



          Set<DomainObject> domainObjSet = myCache.computeIfAbsent(runMode, runModeMap-> new ConcurrentHashMap<>())                
      .computeIfAbsent(bizDate, bizDateMap-> new ConcurrentHashMap<>())
      .computeIfAbsent(bean.getId(), domainSet-> Collections.synchronizedSet(new HashSet<>()).add(bean));

      }









      share|improve this question













      I am new to using Map's computeIfAbsent method. I did find (per Java docs) that the entire method invocation is performed atomically. I would like to know (rather confirm) if the following code snippet (inside the method) would be executed atomically.



           ConcurrentMap<RunMode, Map<LocalDate, Map<Integer, Set<DomainObject>>>> myCache = new ConcurrentHashmap<>();


      public void addToCache (RunMode runMode, LocalDate bizDate, DomainObject bean) {



          Set<DomainObject> domainObjSet = myCache.computeIfAbsent(runMode, runModeMap-> new ConcurrentHashMap<>())                
      .computeIfAbsent(bizDate, bizDateMap-> new ConcurrentHashMap<>())
      .computeIfAbsent(bean.getId(), domainSet-> Collections.synchronizedSet(new HashSet<>()).add(bean));

      }






      java concurrency






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 17:05









      Ram

      62




      62
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          Depends what you mean by atomicity in here.



          It is possible that another threads will get a Map from myCache which wont see entry for bizDate.



          If you want to have fully populated Map from myCache before any thread will get it then you need to create and populate it inside myCache.computeIfAbsent.






          share|improve this answer





















          • thanks for responding. My primary objective is that all the domain objects (beans) must be added to the map, when multiple threads invoke this method. The bizDate & runMode are mostly constant.
            – Ram
            Nov 10 at 22:07













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230309%2fis-concurrenthashmaps-computeifabsent-thread-safe-when-nested%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          Depends what you mean by atomicity in here.



          It is possible that another threads will get a Map from myCache which wont see entry for bizDate.



          If you want to have fully populated Map from myCache before any thread will get it then you need to create and populate it inside myCache.computeIfAbsent.






          share|improve this answer





















          • thanks for responding. My primary objective is that all the domain objects (beans) must be added to the map, when multiple threads invoke this method. The bizDate & runMode are mostly constant.
            – Ram
            Nov 10 at 22:07

















          up vote
          0
          down vote













          Depends what you mean by atomicity in here.



          It is possible that another threads will get a Map from myCache which wont see entry for bizDate.



          If you want to have fully populated Map from myCache before any thread will get it then you need to create and populate it inside myCache.computeIfAbsent.






          share|improve this answer





















          • thanks for responding. My primary objective is that all the domain objects (beans) must be added to the map, when multiple threads invoke this method. The bizDate & runMode are mostly constant.
            – Ram
            Nov 10 at 22:07















          up vote
          0
          down vote










          up vote
          0
          down vote









          Depends what you mean by atomicity in here.



          It is possible that another threads will get a Map from myCache which wont see entry for bizDate.



          If you want to have fully populated Map from myCache before any thread will get it then you need to create and populate it inside myCache.computeIfAbsent.






          share|improve this answer












          Depends what you mean by atomicity in here.



          It is possible that another threads will get a Map from myCache which wont see entry for bizDate.



          If you want to have fully populated Map from myCache before any thread will get it then you need to create and populate it inside myCache.computeIfAbsent.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 9 at 17:29









          tsolakp

          4,46611219




          4,46611219












          • thanks for responding. My primary objective is that all the domain objects (beans) must be added to the map, when multiple threads invoke this method. The bizDate & runMode are mostly constant.
            – Ram
            Nov 10 at 22:07




















          • thanks for responding. My primary objective is that all the domain objects (beans) must be added to the map, when multiple threads invoke this method. The bizDate & runMode are mostly constant.
            – Ram
            Nov 10 at 22:07


















          thanks for responding. My primary objective is that all the domain objects (beans) must be added to the map, when multiple threads invoke this method. The bizDate & runMode are mostly constant.
          – Ram
          Nov 10 at 22:07






          thanks for responding. My primary objective is that all the domain objects (beans) must be added to the map, when multiple threads invoke this method. The bizDate & runMode are mostly constant.
          – Ram
          Nov 10 at 22:07




















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230309%2fis-concurrenthashmaps-computeifabsent-thread-safe-when-nested%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to pass form data using jquery Ajax to insert data in database?

          National Museum of Racing and Hall of Fame

          Guess what letter conforming each word