刚才看了HashTable源码,没有发现HashTable哪里使用同步保证线程安全的?难道是我看错了吗?
public Object put(Object key, Object value) {
// Make sure the value is not null
if (value == null) throw new NullPointerException();
// Makes sure the key is not already in the hashtable.
HashtableEntry e;
HashtableEntry tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
for (e = tab[index] ; e != null ; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
Object old = e.value;
e.value = value;
return old;
}
}
// Rehash the table if the threshold is exceeded
if (count >= threshold) {
rehash();
return put(key, value);
}
// Creates the new entry.
e = new HashtableEntry();
e.hash = hash;
e.key = key;
e.value = value;
e.next = tab[index];
tab[index] = e;
count++;
return null;
}
上面是源码
------解决方案--------------------
好多面试题都说hashmap不是线程安全的,hashtable才是。。。然后我也这么答了。。

------解决方案--------------------
还有arraylist和vector,也是死记硬背的。到现在就没用过vector和hashtable
------解决方案--------------------
一般是较早之前有的都是线程安全的!
------解决方案--------------------

------解决方案--------------------
这个说的是对的,版本比较低的 集合都是 线程安全的。。

------解决方案--------------------
楼主这个根本不是java.util.Hashtable的源码,楼主这个应该是sun自己的内部的com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable的源码。楼主还是看清楚他类所在的包