Java ConcurrentHashMap 在性能方面比 HashMap 更好吗? [英] Java ConcurrentHashMap is better than HashMap performance wise?

查看:30
本文介绍了Java ConcurrentHashMap 在性能方面比 HashMap 更好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在阅读《清洁代码》这本书时看到了这样的声明:

I was just reading the book Clean Code and came across this statement:

Java 年轻的时候,Doug Lea 写了一本开创性的书[8] Concurrent用 Java 编程.随着这本书,他开发了几个线程安全集合,后来成为 JDK 的一部分java.util.concurrent 包.该包中的集合是安全的对于多线程情况,它们表现良好.事实上,ConcurrentHashMap 实现比 HashMap 更好几乎所有情况.它还允许同时并发读取和写入,并且有支持通用复合的方法否则不是线程安全的操作.如果 Java 5 是部署环境,从ConcurrentHashMap

When Java was young Doug Lea wrote the seminal book[8] Concurrent Programming in Java. Along with the book he developed several thread-safe collection, which later became part of the JDK in the java.util.concurrent package. The collections in that package are safe for multithreaded situations and they perform well. In fact, the ConcurrentHashMap implementation performs better than HashMap in nearly all situations. It also allows for simultaneous concurrent reads and writes, and it has methods supporting common composite operations that are otherwise not thread safe. If Java 5 is the deployment environment, start with ConcurrentHashMap

请注意,在上面的引用中,我使用了[n]",其中 n 是某个数字,表示作者提供参考的地方,正如您所看到的,他没有为粗体部分提供任何参考.

Note that in the above quote I used "[n]", where n is some number, to indicate the places where the author provided references, and as you can see he did not provide any reference for the bold part.

不是我不相信这个说法,而是我很想知道这个说法的支持证据.那么,有谁知道显示 ConcurrentHashMapHashMap 的性能统计数据的任何资源?或者谁能​​向我解释为什么 ConcurrentHashMap 比 HashMap 快?

Not that I don't believe this statement, but I would love to know the supporting evidences of this statement. So, does anyone know any resources that shows the performance statistics for both ConcurrentHashMap and HashMap? Or can anyone explain to me why ConcurrentHashMap is faster than HashMap?

我可能会在休息时研究 ConcurrentHashMap 在工作中的实现,但现在我想听听其他 SOers 的答案.

I probably will look into ConcurrentHashMap's implementation at work when I'm taking a break, but for now I would like to hear the answers from fellow SOers.

推荐答案

Doug Lea 非常擅长这些事情,所以如果他的 ConcurrentHashMap 有一次比 Joshua 表现得更好,我不会感到惊讶Bloch 的 HashMap.但是从 Java 7 开始,HashMap 的第一个@author 也变成了 Doug Lea.显然,现在没有理由 HashMap 会比它的并发表亲慢.

Doug Lea is extremely good at these things, so I won't be surprised if at one time his ConcurrentHashMap performs better than Joshua Bloch's HashMap. However as of Java 7, the first @author of HashMap has become Doug Lea too. Obviously now there's no reason HashMap would be any slower than its concurrent cousin.

出于好奇,我还是做了一些基准测试.我在Java 7下运行,条目越多,性能越接近.最终ConcurrentHashMapHashMap的3%以内,非常了不起.瓶颈实际上是内存访问,俗话说内存是新磁盘(磁盘是新磁带)".如果条目在缓存中,两者都会很快;如果条目不适合缓存,两者都会很慢.在实际应用中,地图不一定要很大才能与其他地图竞争驻留在缓存中.如果经常使用地图,则将其缓存;如果没有,它就不会被缓存,这是真正的决定因素,而不是实现(假设两者都是由同一位专家实现的)

Out of curiosity, I did some benchmark anyway. I run it under Java 7. The more entries there are, the closer the performance is. Eventually ConcurrentHashMap is within 3% of HashMap, which is quite remarkable. The bottleneck is really memory access, as the saying goes, "memory is the new disk (and disk is the new tape)". If the entries are in the cache, both will be fast; if the entries don't fit in cache, both will be slow. In real applications, a map doesn't have to be big to compete with others for residing in cache. If a map is used often, it's cached; if not, it's not cached, and that is the real determining factor, not the implementations (given both are implemented by the same expert)

public static void main(String[] args)
{
    for(int i = 0; i<100; i++)
    {
        System.out.println();

        int entries = i*100*1000;
        long t0=test( entries, new FakeMap() );
        long t1=test( entries, new HashMap() );
        long t2=test( entries, new ConcurrentHashMap() );

        long diff = (t2-t1)*100/(t1-t0);
        System.out.printf("entries=%,d time diff= %d%% %n", entries, diff);
    }
}


static long test(int ENTRIES, Map map)
{
    long SEED = 0;
    Random random = new Random(SEED);

    int RW_RATIO = 10;

    long t0 = System.nanoTime();

    for(int i=0; i<ENTRIES; i++)
        map.put( random.nextInt(), random.nextInt() );

    for(int i=0; i<RW_RATIO; i++)
    {
        random.setSeed(SEED);
        for(int j=0; j<ENTRIES; j++)
        {
            map.get( random.nextInt() );
            random.nextInt();
        }
    }
    long t = System.nanoTime()-t0;
    System.out.printf("%,d ns %s %n", t, map.getClass());
    return t;
}


static class FakeMap implements Map
{
    public Object get(Object key)
    {
        return null;  
    }
    public Object put(Object key, Object value)
    {
        return null;  
    }
    // etc. etc.
}

这篇关于Java ConcurrentHashMap 在性能方面比 HashMap 更好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆