hashmap的实现和扩展分析

来源:互联网 发布:优秀的短篇小说知乎 编辑:程序博客网 时间:2024/06/04 01:11

hashmap的实现和扩展分析

  • hashmap为什么并发不安全?
  • 为什么String, Interger这样的wrapper类适合作为键?
  • 自定义hashmap的实现?
  • JUC并发安全的map实现对比测试?
  • hashmap为什么并发不安全?

    1.多线程rehash,多个线程同时进行put()操作,进入transfer()环节.出现环形链表,在进行重新分配大小时,进入死循环.

    2.fail-fast,在使用迭代器的过程中有其他线程修改了map.抛出ConcurrentModificationException异常.

    为什么String, Interger这样的wrapper类适合作为键?

    String, Interger这样的wrapper类作为HashMap的键是再适合不过了,而且String最为常用。因为String是不可变的,也是final的,而且已经重写了equals()和hashCode()方法了。其他的wrapper类也有这个特点。不可变性是必要的,因为为了要计算hashCode(),就要防止键值改变,如果键值在放入时和获取时返回不同的hashcode的话,那么就不能从HashMap中找到你想要的对象。不可变性还有其他的优点如线程安全。如果你可以仅仅通过将某个field声明成final就能保证hashCode是不变的,那么请这么做吧。因为获取对象的时候要用到equals()和hashCode()方法,那么键对象正确的重写这两个方法是非常重要的。如果两个不相等的对象返回不同的hashcode的话,那么碰撞的几率就会小些,这样就能提高HashMap的性能。

    自定义hashmap的实现

    //一个很简单的实现public class MyLinkHashMap {    /**     * 自定义的hashmap,基本元素采用entry[],entry是链表结构. 基本方法: put(string,string)     * get(string)     *      * entry<string,string>     * */    private int length = 10;    private Entry[] entries;    private int index = 0;    public MyLinkHashMap() {        super();        initinalize();    }    public void initinalize() {        entries = new Entry[length];    }    public String put(String key, String value) {        // 由于是链表存储,添加要加到链表头部,所以要获取头        // 第一个问题,放到数组中哪个链表呢,这里通过索引计算法,输入因子是key,通过key一定可以获得value存储在哪个索引下        int index = key.hashCode() % length;        Entry privious = entries[index];        // 第二个问题,如果存在的要覆盖,不存在的才加在头部        for (Entry entry = entries[index]; entry != null; entry = entry.next) {            if (entry.getKey().equals(key)) {                String oldvalue = entry.getValue();                entry.setValue(value);                return oldvalue;            }        }        Entry entry = new Entry(key, value);        entry.next = privious;        entries[index] = entry;        return null;    }    public String get(String key) {        int index = key.hashCode() % length;        for (Entry entry = entries[index]; entry != null; entry = entry.next) {            if (entry.getKey().equals(key)) {                return entry.value;            }        }        return null;    }    public class Entry {        private String key;        private String value;        private Entry next;        public Entry(String key, String value) {            this.key = key;            this.value = value;        }        public String getKey() {            return key;        }        public void setKey(String key) {            this.key = key;        }        public String getValue() {            return value;        }        public void setValue(String value) {            this.value = value;        }        public Entry getNext() {            return next;        }        public void setNext(Entry next) {            this.next = next;        }    }}

    下面的代码进行对比JUC下的并发性能,时间损耗对比

    public class MapCompareTest {    public static Map<String, Integer> hashTable = null;    public static Map<String, Integer> synchronizedMap = null;    public static Map<String, Integer> concurrentHashMap = null;    public static void main(String[] args) throws InterruptedException{        // 构造一个hashtable        hashTable = new Hashtable<>();        performTest(hashTable);        // 构造一个synchronizemap        synchronizedMap = Collections                .synchronizedMap(new HashMap<String, Integer>());        performTest(synchronizedMap);        // 构造一个CHM        concurrentHashMap = new ConcurrentHashMap<>();        performTest(concurrentHashMap);    }    public static void performTest(final Map<String, Integer> map)            throws InterruptedException {        System.out.println("Test started for: " + map.getClass());        long averageTime = 0;        for (int i = 0; i < 5; i++) {            long begintime = System.nanoTime();            ExecutorService service = Executors.newFixedThreadPool(5);            for (int j = 0; j < 5; j++) {                service.execute(new Runnable() {                    @Override                    public void run() {                        Integer number = (int) Math.ceil(Math.random() * 550 * 1000);                        Integer value = map.get(String.valueOf(number));                        map.put(String.valueOf(number), number);                    }                });            }            // 关闭线程池            service.shutdown();            // Blocks until all tasks have completed execution after a shutdown            // request            service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);            long endtime = System.nanoTime();            long totalTime = (endtime - begintime) / 100000L;            averageTime += totalTime;            System.out.println("2500K entried added/retrieved in " + totalTime                    + " ms");        }        System.out.println("For " + map.getClass() + " the average time is "                + averageTime / 5 + " ms\n");    }}

    通过对比分析,在高并发访问的情况下,concurrentHashmap是我们使用的首选.

    [参考文档]

    http://www.importnew.com/22011.html
    http://blog.csdn.net/qq_15771061/article/details/54098023

    原创粉丝点击