공부/자바

[자바] ConcurrentHashMap

ghhong 2022. 12. 27. 16:43

ConcurrentHashMap

HashTable클래스의 대부분 메서드는 synchronized 키워드가 존재하여 메서드가 임계구역으로 설정되어 있다. 이는 Thread-safe하지만 동시에 접근할 병목현상이 발생한다.

HashMap클래스는 synchronized 키워드가 존재하지 않는다. 그렇기에 성능은 좋지만 multi-thread 환경에서는 사용할 없다.

ConcurrentHashMap클래스는 Hashtable클래스의 단점을 보완하며 multi-thread환경에서 사용할 있도록 나온 클래스이다. ConcurrentHashMap클래스는 synchronized키워드가 메서드 전체에 적용되어 있지 않고, put메서드 중간 특정 부분에만 적용되어 있다.

 

public V put(K key, V value) {

        return putVal(key, value, false);

    }



    /** Implementation for put and putIfAbsent */

    final V putVal(K key, V value, boolean onlyIfAbsent) {

        if (key == null || value == null) throw new NullPointerException();

        int hash = spread(key.hashCode());

        int binCount = 0;

        for (Node<K,V>[] tab = table;;) {

            Node<K,V> f; int n, i, fh;

            if (tab == null || (n = tab.length) == 0)

                tab = initTable();

            else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {

                if (casTabAt(tab, i, null,

                             new Node<K,V>(hash, key, value, null)))

                    break;                   // no lock when adding to empty bin

            }

            else if ((fh = f.hash) == MOVED)

                tab = helpTransfer(tab, f);

            else {

                V oldVal = null;

                synchronized (f) {

                    if (tabAt(tab, i) == f) {

                        if (fh >= 0) {

                            binCount = 1;

                            for (Node<K,V> e = f;; ++binCount) {

                                K ek;

                                if (e.hash == hash &&

                                    ((ek = e.key) == key ||

                                     (ek != null && key.equals(ek)))) {

                                    oldVal = e.val;

                                    if (!onlyIfAbsent)

                                        e.val = value;

                                    break;

                                }

                                Node<K,V> pred = e;

                                if ((e = e.next) == null) {

 

 

, 읽기 작업은 여러 쓰레드가 동시에 가능하고, 쓰기는 Lock 얻어 사용한다는 것이다.

 

출처 : https://devlog-wjdrbs96.tistory.com/269

 

[Java] ConcurrentHashMap 이란 무엇일까?

들어가기 전에 HashTable, HashMap, ConcurrnetHashMap은 많이 유사한 특징들을 가지고 있습니다. 하지만 세부적으로 보면 조금씩 꽤나 차이가 있는데요. 간단하게 어떤 차이가 있는지 알아보면서 시작하

devlog-wjdrbs96.tistory.com