title: My ConcurrentHashmap's value type is List,how to make appending to that list thread safe?
body: <p>My class extends from ConcurrentHashmap[String,immutable.List[String]]</p>
  
  <p>and it has 2 methods :</p>
  
  <pre><code>  def addEntry(key: String, newList: immutable.List[String]) = {
      ...
      //if key exist,appending the newList to the exist one 
      //otherwise set the newList as the value
    }
  
    def resetEntry(key: String): Unit = {
      this.remove(key)
    }
  </code></pre>
  
  <p>in order to make the addEntry method thread safe,I tried :</p>
  
  <pre><code>this.get(key).synchronized{
    //append or set here
  }
  </code></pre>
  
  <p>but that will raise null pointer exception if key does not exist,and use putIfAbsent(key, new immutable.List()) before synchronize won't work cause after putIfAbsent and before goes into synchronized block,the key may be removed by resetEntry.</p>
  
  <p>make addEntry and resetEntry both synchronized method will work but the lock is too large</p>
  
  <p>So, what could I do?</p>
  
  <p>ps.this post is similiar with <a href="https://stackoverflow.com/questions/8567596/how-to-make-updating-bigdecimal-within-concurrenthashmap-thread-safe">How to make updating BigDecimal within ConcurrentHashMap thread safe</a> while plz help me figure out how to code other than general guide</p>
  
  <p>--update--
  checkout <a href="https://stackoverflow.com/a/34309186/404145">https://stackoverflow.com/a/34309186/404145</a>, solved this after almost 3+ years later.</p>