Hashtable是线程安全的;先看其构造方法
/**
* Constructs a new, empty hashtable with a default initial capacity (11)
* and load factor (0.75).
*/
public Hashtable() {
this(11, 0.75f);
}
初始大小为11,扩容百分比为0.75;
另外研究期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)
*/
1.public synchronized V put(K key, V value) {
// Make sure the value is not null
2 if (value == null) {
throw new NullPointerException();
}
// Makes sure the key is not already in the hashtable.
3 Entry tab[] = table;
4 int hash = hash(key);
5 int index = (hash & 0x7FFFFFFF) % tab.length;
6 for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {
7 if ((e.hash == hash) && e.key.equals(key)) {
8 V old = e.value;
9 e.value = value;
10 return old;
}
}
11 modCount++;
12 if (count >= threshold) {
// Rehash the table if the threshold is exceeded
13 rehash();
14 tab = table;
15 hash = hash(key);
16 index = (hash & 0x7FFFFFFF) % tab.length;
17 }
// Creates the new entry.
18 Entry<K,V> e = tab[index];
19 tab[index] = new Entry<>(hash, key, value, e);
20 count++;
21 return null;
}
第1行表明它的并发是通过synchronized来加锁;第2行描述value值不能为空;
第4行得到其hash值;第5到10计算put的逻辑过程;
第11-17表明当容量超过因子时,进行扩容的计算;
我的学习暂时到此