Hashtable

在说Hashtable类实现之前,先看一段Hashtable类注释

 //110行
 * Java Collections Framework</a>.  Unlike the new collection
 * implementations, {@code Hashtable} is synchronized.  If a
 * thread-safe implementation is not needed, it is recommended to use
 * {@link HashMap} in place of {@code Hashtable}.  If a thread-safe
 * highly-concurrent implementation is desired, then it is recommended
 * to use {@link java.util.concurrent.ConcurrentHashMap} in place of
 * {@code Hashtable}.

翻译如下:

与新的集合实现不同,Hashtable是同步的。如果不需要线程安全实现,建议使用HashMap代替Hashtable。如果需要线程安全的高并发实现,那么建议使用ConcurrentHashMap代替Hashtable。

 

事实上Hashtable已经不建议使用了,如果没有线程安全问题,推荐使用hashmap,因为hashmap没有同步的限制,效率更高,而如果需要考虑线程安全问题,那么推荐使用ConcurrentHashMap,因为ConcurrentHashMap的锁粒度更小(使用synchronized对索引下标处第一个元素加锁),能支持的并发量更高同时效率也更高。

前面介绍HashMap时,说到HashMap有两处线程安全问题,下面看下Hashtable这两处实现,其他实现基本与hashmap一致。

1,初始化时机

HashMap中是在放入第一个元素时才会初始化,而Hashtable中则是在new时就初始化。

    public Hashtable() {
        this(11, 0.75f);
    }
    
    public Hashtable(int initialCapacity, float loadFactor) {
        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);
    }

可见倒数第二行初始化table。

 

2,put方法。

    /**
     * Maps the specified <code>key</code> to the specified
     * <code>value</code> in this hashtable. Neither the key nor the
     * value can be <code>null</code>. <p>
     *
     * The value can be retrieved by calling the <code>get</code> method
     * with a key that is equal to the original key.
     *
     * @param      key     the hashtable key
     * @param      value   the value
     * @return     the previous value of the specified key in this hashtable,
     *             or <code>null</code> if it did not have one
     * @exception  NullPointerException  if the key or value is
     *               <code>null</code>
     * @see     Object#equals(Object)
     * @see     #get(Object)
     */
    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 = key.hashCode();
        int index = (hash & 0x7FFFFFFF) % tab.length;
        @SuppressWarnings("unchecked")
        Entry<K,V> entry = (Entry<K,V>)tab[index];
        for(; entry != null ; entry = entry.next) {
            if ((entry.hash == hash) && entry.key.equals(key)) {
                V old = entry.value;
                entry.value = value;
                return old;
            }
        }

        addEntry(hash, key, value, index);
        return null;
    }

可见put方法整个使用synchronized修饰保证线程安全。

 

3,key和value不能为空

从上面put方法注释或方法实现中可看出key和value都不能为null,否则抛出空指针异常;

key:key.hashCode();

value:if(value==null) throw NullPointerException;

 

4,索引计算方式不同。

int index = (hash & 0x7FFFFFFF) % tab.length;

使用key的hashcode与int最大值做与运算的结果对table容量(注意不是table元素个数)求余。

 

其他操作的方法同样均使用synchronized实现。

更多jdk源码介绍​​​​​​​

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值