jdk-hashTable

今天光顾一下hashtable吧,其实又了之前hashmap和Concurrenthashmap的源码分析,再看hashtable,就显的简单多了。

public Hashtable(int initialCapacity, float loadFactor) {
        //hashtable中默认值是11,也就是table的大小默认为11 loadFactor依然是0.75f
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                    initialCapacity);
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal Load: "+loadFactor);

        if (initialCapacity==0)
            initialCapacity = 1;
        this.loadFactor = loadFactor;
        table = new Entry[initialCapacity];
        threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
        useAltHashing = sun.misc.VM.isBooted() &&
                (initialCapacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD);
    }

可以看见对于hashtable来说,它的同步都是加在方法级别,put和get都是同步的,这就导致在写的时候不能读,读的时候不能写,还是很费时的。

public synchronized V put(K key, V value) {
        // Make sure the value is not null
        if (value == null) {
            throw new NullPointerException();
        }
        // Makes sure the key is not already in the hashtable.
        Entry tab[] = table;
        int hash = hash(key);
        int index = (hash & 0x7FFFFFFF) % tab.length;   //可以看见每个集合的定位index不同,但是结果一致
        for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {
            //定位到在哪个桶内之后,遍历桶中的entry,排除null值
            if ((e.hash == hash) && e.key.equals(key)) {
                V old = e.value;
                e.value = value;
                return old;  //返回旧值
            }
        }
        modCount++;
        if (count >= threshold) {
            //这边执行扩容,不去关心
            // Rehash the table if the threshold is exceeded
            rehash();

            tab = table;
            hash = hash(key);
            index = (hash & 0x7FFFFFFF) % tab.length;
        }

        // Creates the new entry.
        Entry<K,V> e = tab[index]; //扩容之后将原有index的值放在新的hash值处
        tab[index] = new Entry<>(hash, key, value, e);
        count++;
        return null;
    }

public synchronized V get(Object key) {
        Entry tab[] = table;
        int hash = hash(key);
        int index = (hash & 0x7FFFFFFF) % tab.length;
        for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {
            if ((e.hash == hash) && e.key.equals(key)) {
                return e.value;
            }
        }
        return null;
    }

对于之前详细分析过hashmap和ConcurrentHashMap来说的话,看table真实一眼看到底,其实没什么区别的,只要注意它的锁是加在整个table上的,ConcurrentHashMap是加在Segment处的,更快一些。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值