Java ConcurrentHashMap

Java ConcurrentHashMap

Sample ConcurrentHashMap

The ConcurrentHashMap class in Java is a thread-safe implementation of the Map interface. This means it allows multiple threads to access and modify its contents concurrently without throwing ConcurrentModificationException errors.

Here are some key points about ConcurrentHashMap:

  • Thread-safety: It achieves thread-safety through a technique called concurrency control. This involves using mechanisms like locks or compare-and-swap (CAS) operations to ensure that only one thread can modify the map at a time, preventing data corruption.
  • Performance: Compared to the traditional HashMap, ConcurrentHashMap has slightly lower performance due to the overhead of concurrency control. However, it is still a good choice for most multi-threaded applications due to its thread-safety guarantees.
  • Use cases: It is ideal for situations where multiple threads need to access and modify a shared map concurrently, such as: Caching data Implementing concurrent data structures Managing shared resources in multi-threaded applications


Without concurrent ConcurrentHashMap, we will get exception

java.util.ConcurrentModificationException


public class ConcurrentHashMapSample {

    public static void main(String[] args) {

        Map<String, Object> animals = new HashMap<String, Object>();
        animals.put("penguin", 1);
        animals.put("flamingo", 2);
        animals.put("tiger", 3);

        for(Map.Entry<String, Object> entry: animals.entrySet()) {
            if (entry.getKey().equals("tiger")) {
                animals.remove("tiger");
            }
        }

        for(Map.Entry<String, Object> entry: animals.entrySet()) {
            System.out.println("==key:"+entry.getKey());
        }

    }
}        


With ConcurrentHashMap

public class ConcurrentHashMapSample {

    public static void main(String[] args) {

        Map<String, Object> animals = new ConcurrentHashMap<>();
        animals.put("penguin", 1);
        animals.put("flamingo", 2);
        animals.put("tiger", 3);

        for(Map.Entry<String, Object> entry: animals.entrySet()) {
            if (entry.getKey().equals("tiger")) {
                animals.remove("tiger");
            }
        }

        for(Map.Entry<String, Object> entry: animals.entrySet()) {
            System.out.println("==key:"+entry.getKey());
        }

    }
}        


The code run success and we get the result


==key:penguin
==key:flamingo



--end


要查看或添加评论,请登录

Tung Vo的更多文章

  • MD5 message-digest algorithm

    MD5 message-digest algorithm

    MD5 (Message-Digest Algorithm 5) is a widely used cryptographic hash function that generates a fixed-length 128-bit…

  • Oracle UTL_SMTP send mail with TLS

    Oracle UTL_SMTP send mail with TLS

    This article discribes send email with TLS by using Oracle UTL_SMTP Transport Layer Security (TLS) WIKI is a…

  • Mahalanobis distance and classifier

    Mahalanobis distance and classifier

    This article illustrates classify the pattern using Mahalanobis distance sampe variance sampe variance Sample…

  • Oracle ADF Show detail viewObject by request URL

    Oracle ADF Show detail viewObject by request URL

    Show detail viewObject by request URL Install Java Install JDeveloper Open JDeveloper, choose New Application..

  • Oracle ADF fetch detail ViewObject by request URL

    Oracle ADF fetch detail ViewObject by request URL

    This article address how to fetch detail data of View object by request parameter Install Java Install JDeveloper Open…

    1 条评论
  • Oracle ADF Binding value dropdown to transient attributes

    Oracle ADF Binding value dropdown to transient attributes

    This article Oracle ADF Binding value dropdown to transient attributes Install Java Install JDeveloper Open Jdeveloper…

  • SOLID Principles

    SOLID Principles

    SOLID là vi?t t?t c?a 5 nguyên t?c thi?t k? h??ng ??i t??ng (OOP) giúp cho code d? ??c, d? hi?u, d? b?o trì h?n. Các…

  • Java parallelStream

    Java parallelStream

    Introduced in Java 8, parallel streams are a powerful tool for performing operations on collections in a concurrent and…

  • Java ExecutorService

    Java ExecutorService

    Interface ExecutorService of package java.util.

  • JAVA synchronized

    JAVA synchronized

    JAVA synchronized sample Create calculator class Create main class using this Calculator class Run code we will have…

社区洞察

其他会员也浏览了