问题描述
我刚刚在阅读《清洁代码》这本书时遇到了这样的说法:
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, theConcurrentHashMap
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 withConcurrentHashMap
请注意,在上面的引用中,我使用了[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 非常擅长这些事情,所以如果有一次他的 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 下运行它.条目越多,性能越接近.最终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 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!