Java ConcurrentSkipListSet 什么时候有用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1904439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:42:41  来源:igfitidea点击:

When is a ConcurrentSkipListSet useful?

javadata-structurescollectionsconcurrency

提问by andandandand

I just saw this data-structure on the Java 6 API and I'm curious about when it would be an useful resource. I'm studying for the scjp exam and I don't see it covered on Kathy Sierra's book, even though I've seen mock exam questions that mention it.

我刚刚在 Java 6 API 上看到了这个数据结构,我很好奇它什么时候会成为有用的资源。我正在为 scjp 考试而学习,但我在 Kathy Sierra 的书中没有看到它,尽管我看过提到它的模拟考试题。

采纳答案by Todd Gamblin

ConcurrentSkipListSetand ConcurrentSkipListMapare useful when you need a sorted container that will be accessed by multiple threads. These are essentially the equivalents of TreeMap and TreeSet for concurrent code.

ConcurrentSkipListSetConcurrentSkipListMap当您需要一个可以被多个线程访问的排序容器时非常有用。对于并发代码,这些本质上是 TreeMap 和 TreeSet 的等价物。

The implementation for JDK 6 is based on High Performance Dynamic Lock-Free Hash Tables and List-Based Setsby Maged Michael at IBM, which shows that you can implement a lot of operations on skip lists atomically using compare and swap (CAS)operations. These are lock-free, so you don't have to worry about the overhead of synchronized(for most operations) when you use these classes.

JDK 6 的实现基于IBM 的 Maged Michael 的High Performance Dynamic Lock-Free Hash Tables and List-Based Sets,这表明您可以使用比较和交换 (CAS)操作原子地在跳过列表上实现许多操作。这些是无锁的,因此您synchronized在使用这些类时不必担心(对于大多数操作)的开销。

There's currently no Red-Black treebased concurrent Map/Set implementation in Java. I looked through the literature a bit and found a couplepapersthat showed concurrent RB trees outperforming skip lists, but a lot of these tests were done with transactional memory, which isn't supported in hardware on any major architectures at the moment.

目前在 Java 中没有基于红黑树的并发 Map/Set 实现。我通过文献查了一下,发现一对夫妇的论文即表明并发RB树跑赢跳跃列表,但很多这些测试都是用做事务性的存储器,这是不是在硬件支持目前任何主要的架构。

I'm assuming the JDK guys went with a skip list here because the implementation was well-known and because making it lock-free was simple and portable (using CAS). If anyone cares to clarify, please do. I'm curious.

我假设 JDK 人员在这里使用了一个跳过列表,因为实现是众所周知的,并且因为使其无锁简单且可移植(使用 CAS)。如果有人愿意澄清,请做。我很好奇。

回答by irreputable

skip lists are sorted lists, and efficient to modify with log(n) performance. in that regard it's like TreeSet. however there is no ConcurrentTreeSet. what I heard is that skip list is very easy to implement, that's probably why.

跳过列表是排序列表,可以有效地修改 log(n) 性能。在这方面,它就像 TreeSet。但是没有 ConcurrentTreeSet。我听说跳过列表很容易实现,这可能就是原因。

Anyway, when you need a concurrent, sorted and efficient set, you can use ConcurrentSkipListSet

无论如何,当你需要一个并发的、有序的、高效的集合时,你可以使用 ConcurrentSkipListSet

回答by Kaleb Brasee

These are useful when you need a set that can safely be accessed by multiple threads simultaneously. It also provides decent performance by being weakly consistent -- inserts can be made safely while you're iterating through the Set, but there's no guarantee that your Iterator will see that insert.

当您需要一个可以被多个线程同时安全访问的集合时,这些非常有用。它还通过弱一致性提供了不错的性能——在您遍历 Set 时可以安全地进行插入,但不能保证您的迭代器会看到该插入。

回答by Patrick Rusk

ConcurrentSkipListMap was a fantastic find when I needed to implement a replication layer for a home-grown cache. The Map aspects implemented the cache, and the underlying List aspects let me keep track of the order in which objects appeared in the cache. The "skip" aspect of that list made it efficient to remove an object from one spot in the list and bump it to the end when it was replaced in the cache.

当我需要为本地缓存实现复制层时,ConcurrentSkipListMap 是一个很棒的发现。Map 方面实现了缓存,底层的 List 方面让我可以跟踪对象出现在缓存中的顺序。该列表的“跳过”方面使得从列表中的一个位置删除一个对象并在缓存中替换它时将其撞到末尾变得高效。