Hashtable还是线程安全的吗

u011165933 2017-07-31 05:24:16
在查看JDK1.6中Hashtable的源码时,发现所有的方法都没有synchronized关键字修饰,而且源码中有如下注释:
/**
* IMPORTANT NOTE:
* This code was taken from Sun's Java1.1 JDK java.util.HashTable.java
* All "synchronized" keywords and some methods we do not need have been
* all been removed.
*/

请问各位,现在的Hashtable是不是也并不是线程安全的了?为什么不再有synchronized修饰了?在网上也没查到什么,有没有什么相关资料可以给我推荐我阅读一下的,谢谢!
...全文
434 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
李德胜1995 2017-07-31
  • 打赏
  • 举报
回复
引用 11 楼 u011165933 的回复:
噢,我明白怎么回事了,我看的是com.sun.org.apache.xalan.internal.xsltc.runtime的Hashtable类,没注意是这个包下的,醉了
笑死。。。
u011165933 2017-07-31
  • 打赏
  • 举报
回复
引用 12 楼 soton_dolphin 的回复:
[quote=引用 9 楼 u011165933 的回复:] [quote=引用 8 楼 soton_dolphin 的回复:] [quote=引用 3 楼 u011165933 的回复:] [quote=引用 1 楼 soton_dolphin 的回复:] 一直都是线程安全的,你去API上一查就看到了 http://docs.oracle.com/javase/8/docs/api/ As of the Java 2 platform v1.2, this class was retrofitted to implement the Map interface, making it a member of the Java Collections Framework. Unlike the new collection implementations, Hashtable is synchronized. If a thread-safe implementation is not needed, it is recommended to use HashMap in place of Hashtable. If a thread-safe highly-concurrent implementation is desired, then it is recommended to use ConcurrentHashMap in place of Hashtable.
还请问下,那为什么现在没有synchronized关键字修饰了,如何保证线程安全的呢?[/quote] 你告诉我你在哪里看到的没有synchronized关键字啊??[/quote] 比如put和get方法:
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;
    }
public Object get(Object key) {
        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))
                return e.value;

        return null;
    }
[/quote] 你看的是盗版API?

 /**
     * 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;
    }

[/quote] 我把包看错了,太粗心了看的不是util包下的,没有问题了
soton_dolphin 2017-07-31
  • 打赏
  • 举报
回复
引用 9 楼 u011165933 的回复:
[quote=引用 8 楼 soton_dolphin 的回复:] [quote=引用 3 楼 u011165933 的回复:] [quote=引用 1 楼 soton_dolphin 的回复:] 一直都是线程安全的,你去API上一查就看到了 http://docs.oracle.com/javase/8/docs/api/ As of the Java 2 platform v1.2, this class was retrofitted to implement the Map interface, making it a member of the Java Collections Framework. Unlike the new collection implementations, Hashtable is synchronized. If a thread-safe implementation is not needed, it is recommended to use HashMap in place of Hashtable. If a thread-safe highly-concurrent implementation is desired, then it is recommended to use ConcurrentHashMap in place of Hashtable.
还请问下,那为什么现在没有synchronized关键字修饰了,如何保证线程安全的呢?[/quote] 你告诉我你在哪里看到的没有synchronized关键字啊??[/quote] 比如put和get方法:
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;
    }
public Object get(Object key) {
        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))
                return e.value;

        return null;
    }
[/quote] 你看的是盗版API?

 /**
     * 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;
    }

u011165933 2017-07-31
  • 打赏
  • 举报
回复
噢,我明白怎么回事了,我看的是com.sun.org.apache.xalan.internal.xsltc.runtime的Hashtable类,没注意是这个包下的,醉了
u011165933 2017-07-31
  • 打赏
  • 举报
回复
引用 7 楼 pany1209 的回复:
[quote=引用 6 楼 u011165933 的回复:] [quote=引用 5 楼 pany1209 的回复:] [quote=引用 4 楼 u011165933 的回复:] [quote=引用 2 楼 pany1209 的回复:]
我太菜了,大神给我解释下好呗[/quote] 你1.6的源码看不到synchronized???[/quote] 打错了是1.7[/quote] 1.7没有??哪里看的???[/quote] JDK源码中啊,没见到synchronized关键字,其中注释All "synchronized" keywords have been removed?
u011165933 2017-07-31
  • 打赏
  • 举报
回复
引用 8 楼 soton_dolphin 的回复:
[quote=引用 3 楼 u011165933 的回复:] [quote=引用 1 楼 soton_dolphin 的回复:] 一直都是线程安全的,你去API上一查就看到了 http://docs.oracle.com/javase/8/docs/api/ As of the Java 2 platform v1.2, this class was retrofitted to implement the Map interface, making it a member of the Java Collections Framework. Unlike the new collection implementations, Hashtable is synchronized. If a thread-safe implementation is not needed, it is recommended to use HashMap in place of Hashtable. If a thread-safe highly-concurrent implementation is desired, then it is recommended to use ConcurrentHashMap in place of Hashtable.
还请问下,那为什么现在没有synchronized关键字修饰了,如何保证线程安全的呢?[/quote] 你告诉我你在哪里看到的没有synchronized关键字啊??[/quote] 比如put和get方法:
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;
    }
public Object get(Object key) {
        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))
                return e.value;

        return null;
    }
soton_dolphin 2017-07-31
  • 打赏
  • 举报
回复
引用 3 楼 u011165933 的回复:
[quote=引用 1 楼 soton_dolphin 的回复:] 一直都是线程安全的,你去API上一查就看到了 http://docs.oracle.com/javase/8/docs/api/ As of the Java 2 platform v1.2, this class was retrofitted to implement the Map interface, making it a member of the Java Collections Framework. Unlike the new collection implementations, Hashtable is synchronized. If a thread-safe implementation is not needed, it is recommended to use HashMap in place of Hashtable. If a thread-safe highly-concurrent implementation is desired, then it is recommended to use ConcurrentHashMap in place of Hashtable.
还请问下,那为什么现在没有synchronized关键字修饰了,如何保证线程安全的呢?[/quote] 你告诉我你在哪里看到的没有synchronized关键字啊??
李德胜1995 2017-07-31
  • 打赏
  • 举报
回复
引用 6 楼 u011165933 的回复:
[quote=引用 5 楼 pany1209 的回复:] [quote=引用 4 楼 u011165933 的回复:] [quote=引用 2 楼 pany1209 的回复:]
我太菜了,大神给我解释下好呗[/quote] 你1.6的源码看不到synchronized???[/quote] 打错了是1.7[/quote] 1.7没有??哪里看的???
u011165933 2017-07-31
  • 打赏
  • 举报
回复
引用 5 楼 pany1209 的回复:
[quote=引用 4 楼 u011165933 的回复:] [quote=引用 2 楼 pany1209 的回复:]
我太菜了,大神给我解释下好呗[/quote] 你1.6的源码看不到synchronized???[/quote] 打错了是1.7
李德胜1995 2017-07-31
  • 打赏
  • 举报
回复
引用 4 楼 u011165933 的回复:
[quote=引用 2 楼 pany1209 的回复:]
我太菜了,大神给我解释下好呗[/quote] 你1.6的源码看不到synchronized???
u011165933 2017-07-31
  • 打赏
  • 举报
回复
引用 2 楼 pany1209 的回复:
我太菜了,大神给我解释下好呗
u011165933 2017-07-31
  • 打赏
  • 举报
回复
引用 1 楼 soton_dolphin 的回复:
一直都是线程安全的,你去API上一查就看到了 http://docs.oracle.com/javase/8/docs/api/ As of the Java 2 platform v1.2, this class was retrofitted to implement the Map interface, making it a member of the Java Collections Framework. Unlike the new collection implementations, Hashtable is synchronized. If a thread-safe implementation is not needed, it is recommended to use HashMap in place of Hashtable. If a thread-safe highly-concurrent implementation is desired, then it is recommended to use ConcurrentHashMap in place of Hashtable.
还请问下,那为什么现在没有synchronized关键字修饰了,如何保证线程安全的呢?
李德胜1995 2017-07-31
  • 打赏
  • 举报
回复
soton_dolphin 2017-07-31
  • 打赏
  • 举报
回复
一直都是线程安全的,你去API上一查就看到了 http://docs.oracle.com/javase/8/docs/api/ As of the Java 2 platform v1.2, this class was retrofitted to implement the Map interface, making it a member of the Java Collections Framework. Unlike the new collection implementations, Hashtable is synchronized. If a thread-safe implementation is not needed, it is recommended to use HashMap in place of Hashtable. If a thread-safe highly-concurrent implementation is desired, then it is recommended to use ConcurrentHashMap in place of Hashtable.

62,614

社区成员

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

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