HashMap 和 Hashtable

本贴最后更新于 2066 天前,其中的信息可能已经沧海桑田
HashMap 和 Hashtable 区别
  • HashMap 线程不安全,操作不是同步的,而 Hashtable 是线程安全,里面的操作时同步的(一些常用方法都用了 synchronized 标识符,如 put,get 等方法)
  • HashMap 碰到哈希冲突时是将新的 Node 放在 array[index].nextHashtable 发生冲突时是将新的 Node 放在 array[index],将 old array[index] 放在 array[index].next
线程安全的验证
package thread;

import java.util.*;

/**
 * @author:ace
 * @date:2018-08-21
 */
public class ThreadDemo {
    enum MapType{
        //HashMap
        HM,
        //Hashtable
        HT
    }
    public static void main(String[] args) {
        for (int i=0;i<20;i++){
            test(MapType.HT);
        }
    }
    private static void test(MapType type){
        try {

            //List<Integer> list = new ArrayList<>();
            Map<Integer,Integer> map;
            if(type==MapType.HM)
                map=new HashMap<>();
            else if(type==MapType.HT)
                map=new Hashtable<>();
            else
                map=null;
            //Hashtable<Integer, Integer> hashtable = new Hashtable<>();
            for (int i = 0; i < 100; i++) {
                //list.add(i);
                map.put(i,i);
            }
            Thread thread1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        /*
                        while (list.size() > 0) {
                            System.out.println(list.get(0));
                            list.remove(0);
                            Thread.sleep(100);
                        }*/
                        for (int i = 0; i < 1000; i++) {
                            map.put(i,i);
                        }
                        System.out.println("thread1: "+ map.get(500));
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            });
            Thread thread2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        /*
                        while (list.size() > 0) {
                            System.out.println(list.get(0));
                            list.remove(0);
                            Thread.sleep(100);
                        }*/
                        for (int i = 1000; i < 2000; i++) {
                            map.put(i,i);
                        }
                        System.out.println("thread2: "+map.get(1500));
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            });
            thread1.start();
            thread2.start();

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}

当 map 为 HashMap 时,会出现为 null 的情况,因为不同的线程操作 HashMap 时不同步。map 为 Hashtable 时都是正常添加正常取出的,但是 Hashtable 会影响性能。

支持同步的 HashMap

推荐 ConcurrentHashMap 类来做同步处理

  • Java

    Java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由 Sun Microsystems 公司于 1995 年 5 月推出的。Java 技术具有卓越的通用性、高效性、平台移植性和安全性。

    3167 引用 • 8207 回帖 • 1 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...