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

查看:382
本文介绍了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.

不是我不相信这句话,但我很想知道这句话的支持证据。那么,有没有人知道任何资源显示 ConcurrentHashMap HashMap 的性能统计信息?或者任何人都可以向我解释为什么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在这些方面是非常好的,所以我不会感到惊讶,如果一次他的ConcurrentyHashMap执行比Joshua Bloch的HashMap。但是从Java 7开始,H​​ashMap的第一个@author也成为Doug Lea。显然现在没有理由HashMap会比任何比并发表达更慢。

Doug Lea is extremely good at these things, so I won't be surprised if at one time his ConcurrentyHashMap performas 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下运行它。条目越多,性能就越接近。最终ConcurrentHashMap在HashMap的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天全站免登陆