5 Replies Latest reply: Jul 19, 2012 1:33 PM by 900885 RSS

    Query regarding ConcurrentHashMap putIfAbsent() method

    AjayL
      Folks,

      I have a question regarding ConcurrentHashMap:

      The synchronization wrappers add automatic synchronization (thread-safety) to a collection
      public  static <K,V>  Map<K,V> synchronizedMap(Map<K,V> m);
      In effect the put(K,V) operation of the Map is now atomic and is accessible by one thread at a time.

      The ConcurrentHashMap class has a method called putIfAbsent(K key, V value)
      This operation is also atomic in respect to concurrency.

      My question is:

      What is the advantage of PutIfAbsent() method in ConcurrentHashMap() if the method put() also is atomic using SynchronizedMap?
      How does this differ from SynchronizedMap that also has the put() method atomic?

      In the face of concurrent access,I can use the SynchronizedMap to put a value in the Map isnt it?

      Any pointers please?

      Thanks