HashTable真的是线程安全吗?

白日梦桑 2014-03-01 09:53:01
刚才看了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;
}
上面是源码
...全文
872 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Emor_smile 2014-12-29
  • 打赏
  • 举报
回复
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; for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) { 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; index = (hash & 0x7FFFFFFF) % tab.length; } // Creates the new entry. Entry<K,V> e = tab[index]; tab[index] = new Entry<K,V>(hash, key, value, e); count++; return null; } /** * Removes the key (and its corresponding value) from this * hashtable. This method does nothing if the key is not in the hashtable. * * @param key the key that needs to be removed * @return the value to which the key had been mapped in this hashtable, * or <code>null</code> if the key did not have a mapping * @throws NullPointerException if the key is <code>null</code> */ public synchronized V remove(Object key) { Entry tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; for (Entry<K,V> e = tab[index], prev = null ; e != null ; prev = e, e = e.next) { if ((e.hash == hash) && e.key.equals(key)) { modCount++; if (prev != null) { prev.next = e.next; } else { tab[index] = e.next; } count--; V oldValue = e.value; e.value = null; return oldValue; } } return null; } 这是1.6的源码
乔不思 2014-03-04
  • 打赏
  • 举报
回复
引用 4 楼 ganshenml 的回复:
一般是较早之前有的都是线程安全的!
这个说的是对的,版本比较低的 集合都是 线程安全的。。
suciver 2014-03-04
  • 打赏
  • 举报
回复
楼主这个根本不是java.util.Hashtable的源码,楼主这个应该是sun自己的内部的com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable的源码。楼主还是看清楚他类所在的包
ay转身遇 2014-03-03
  • 打赏
  • 举报
回复
必须是安全的。
rockets311 2014-03-02
  • 打赏
  • 举报
回复
请问楼主这段代码是哪个版本的jdk的?怎么看着这么不对劲儿呢?
ganshenml 2014-03-02
  • 打赏
  • 举报
回复
一般是较早之前有的都是线程安全的!
白日梦桑 2014-03-02
  • 打赏
  • 举报
回复
唉。。。面试的特点就是不靠谱
  • 打赏
  • 举报
回复
还有arraylist和vector,也是死记硬背的。到现在就没用过vector和hashtable
  • 打赏
  • 举报
回复
好多面试题都说hashmap不是线程安全的,hashtable才是。。。然后我也这么答了。。

81,087

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧