我什么时候应该使用ConcurrentSkipListMap?
-
06-07-2019 - |
题
在Java中, ConcurrentHashMap
用于更好的多线程
解决方案。那我什么时候应该使用 ConcurrentSkipListMap
?这是一种冗余吗?
这两者之间的多线程方面是否常见?
解决方案
这两个类别在某些方面有所不同。
ConcurrentHashMap 不保证*作为合同的一部分,其运营的运行时间。它还允许调整某些负载因子(大致,同时修改它的线程数)。
ConcurrentSkipListMap ,关于另一方面,保证了各种操作的平均O(log(n))性能。它也不支持为并发调整。 ConcurrentSkipListMap
还有许多 ConcurrentHashMap
没有的操作:ceilingEntry / Key,floorEntry / Key等。它还维护一个排序顺序,否则必须是如果您使用 ConcurrentHashMap
,则计算(以显着的费用)。
基本上,为不同的用例提供了不同的实现。如果您需要快速单键/值对添加和快速单键查找,请使用 HashMap
。如果您需要更快的有序遍历,并且可以承担额外的插入成本,请使用 SkipListMap
。
*虽然我希望实现大致符合O(1)插入/查找的一般哈希映射保证;忽略重新散列
其他提示
有关数据结构的定义,请参见跳过列表。 ConcurrentSkipListMap以其键的自然顺序(或您定义的其他键顺序)存储Map。所以它的get / put / contains操作比HashMap慢,但为了抵消它,它支持SortedMap和NavigableMap接口。
在性能方面,当用作Map时, skipList
似乎慢了10-20倍。这是我的测试结果(Java 1.8.0_102-b14,win x32)
Benchmark Mode Cnt Score Error Units
MyBenchmark.hasMap_get avgt 5 0.015 ? 0.001 s/op
MyBenchmark.hashMap_put avgt 5 0.029 ? 0.004 s/op
MyBenchmark.skipListMap_get avgt 5 0.312 ? 0.014 s/op
MyBenchmark.skipList_put avgt 5 0.351 ? 0.007 s/op
除此之外 - 比较一对一的用例确实有意义。使用这两个集合实现最近最近使用的项目的缓存。现在,skipList的效率看起来更加可疑。
MyBenchmark.hashMap_put1000_lru avgt 5 0.032 ? 0.001 s/op
MyBenchmark.skipListMap_put1000_lru avgt 5 3.332 ? 0.124 s/op
以下是JMH的代码(执行为 java -jar target / benchmarks.jar -bm avgt -f 1 -wi 5 -i 5 -t 1
)
static final int nCycles = 50000;
static final int nRep = 10;
static final int dataSize = nCycles / 4;
static final List<String> data = new ArrayList<>(nCycles);
static final Map<String,String> hmap4get = new ConcurrentHashMap<>(3000, 0.5f, 10);
static final Map<String,String> smap4get = new ConcurrentSkipListMap<>();
static {
// prepare data
List<String> values = new ArrayList<>(dataSize);
for( int i = 0; i < dataSize; i++ ) {
values.add(UUID.randomUUID().toString());
}
// rehash data for all cycles
for( int i = 0; i < nCycles; i++ ) {
data.add(values.get((int)(Math.random() * dataSize)));
}
// rehash data for all cycles
for( int i = 0; i < dataSize; i++ ) {
String value = data.get((int)(Math.random() * dataSize));
hmap4get.put(value, value);
smap4get.put(value, value);
}
}
@Benchmark
public void skipList_put() {
for( int n = 0; n < nRep; n++ ) {
Map<String,String> map = new ConcurrentSkipListMap<>();
for( int i = 0; i < nCycles; i++ ) {
String key = data.get(i);
map.put(key, key);
}
}
}
@Benchmark
public void skipListMap_get() {
for( int n = 0; n < nRep; n++ ) {
for( int i = 0; i < nCycles; i++ ) {
String key = data.get(i);
smap4get.get(key);
}
}
}
@Benchmark
public void hashMap_put() {
for( int n = 0; n < nRep; n++ ) {
Map<String,String> map = new ConcurrentHashMap<>(3000, 0.5f, 10);
for( int i = 0; i < nCycles; i++ ) {
String key = data.get(i);
map.put(key, key);
}
}
}
@Benchmark
public void hasMap_get() {
for( int n = 0; n < nRep; n++ ) {
for( int i = 0; i < nCycles; i++ ) {
String key = data.get(i);
hmap4get.get(key);
}
}
}
@Benchmark
public void skipListMap_put1000_lru() {
int sizeLimit = 1000;
for( int n = 0; n < nRep; n++ ) {
ConcurrentSkipListMap<String,String> map = new ConcurrentSkipListMap<>();
for( int i = 0; i < nCycles; i++ ) {
String key = data.get(i);
String oldValue = map.put(key, key);
if( (oldValue == null) && map.size() > sizeLimit ) {
// not real lru, but i care only about performance here
map.remove(map.firstKey());
}
}
}
}
@Benchmark
public void hashMap_put1000_lru() {
int sizeLimit = 1000;
Queue<String> lru = new ArrayBlockingQueue<>(sizeLimit + 50);
for( int n = 0; n < nRep; n++ ) {
Map<String,String> map = new ConcurrentHashMap<>(3000, 0.5f, 10);
lru.clear();
for( int i = 0; i < nCycles; i++ ) {
String key = data.get(i);
String oldValue = map.put(key, key);
if( (oldValue == null) && lru.size() > sizeLimit ) {
map.remove(lru.poll());
lru.add(key);
}
}
}
}
ConcurrentHashMap:当你想要基于多线程索引的get / put时,只支持基于索引的操作。获取/放置是O(1)
ConcurrentSkipListMap:比get / put更多的操作,比如按键排序的顶部/底部n项,获取最后一个条目,获取/遍历按键排序的整个地图等。复杂性为O(log(n)),所以放性能不如ConcurrentHashMap。它不是带有SkipList的ConcurrentNavigableMap的实现。
总结使用ConcurrentSkipListMap时,如果要在需要排序功能的地图上执行更多操作,而不仅仅是简单的get和put。