简体   繁体   English

ConcurrentSkipList? 也就是说,不是ConcurrentSkipListSet

[英]ConcurrentSkipList? That is, not a ConcurrentSkipListSet

I need a very fast (insert, remove, contains) highly concurrent list that can be sorted using a comparator/comparable. 我需要一个非常快速的(插入,删除,包含)高度并发的列表 ,可以使用比较器/可比较器对其进行排序。

The existing ConcurrentSkipListSet would be ideal, if it was a list and not a set. 如果现有的ConcurrentSkipListSet是列表而不是集合,则将是理想的。 I need to insert multiple items which are equal into the data structure. 我需要在数据结构中插入相等的多个项目。

I'm currently thinking of using a LinkedDeque if I can't find anything better, but that structure is considerably slower than a skiplist at high contention. 我目前正在考虑使用LinkedDeque,如果找不到更好的方法,但是该结构比竞争激烈时的跳过列表要慢得多。

Any suggestions? 有什么建议么?

EDIT: What I actually need, bare minimum, is something that is sorted using compareTo, can insert concurrently and can remove/get items using object identity. 编辑:最低限度,我实际需要的是使用compareTo进行排序的东西,可以同时插入并可以使用对象标识删除/获取项目。 All other concurrent requirements mentioned in comments still apply. 注释中提到的所有其他并行要求仍然适用。

The existing ConcurrentSkipListSet would be ideal, if it was a list and not a set. 如果现有的ConcurrentSkipListSet是列表而不是集合,则将是理想的。

So the SkipList data-structure at it's core is a linked list. 因此, SkipList数据结构的核心是一个链表。 If you are worried about order and the ability to traverse it easily and in order, the SkipList will work very well for that as well. 如果您担心订单以及能否轻松,有序地遍历订单,则SkipList对此也将非常适用。 It is also a probabilistic alternative to a balanced tree which is why it can also be a Set or a Map . 它也是平衡树的概率替代方案,这就是为什么它也可以是SetMap The data structure in memory looks something like the following: 内存中的数据结构如下所示:

在此处输入图片说明

To quote from the Javadocs: 引用Javadocs:

This class implements a concurrent variant of SkipLists providing expected average log(n) time cost for the containsKey, get, put and remove operations and their variants. 此类实现了SkipLists的并发变体,从而为containsKey,get,put和remove操作及其变体提供了预期的平均log(n)时间成本。 Insertion, removal, update, and access operations safely execute concurrently by multiple threads. 插入,删除,更新和访问操作可以安全地由多个线程同时执行。 Iterators are weakly consistent, returning elements reflecting the state of the map at some point at or since the creation of the iterator. 迭代器是弱一致性的,返回的元素在创建迭代器时或创建迭代器后的某个时刻反映地图的状态。 They do not throw ConcurrentModificationException, and may proceed concurrently with other operations. 它们不会引发ConcurrentModificationException,并且可以与其他操作并发进行。 Ascending key ordered views and their iterators are faster than descending ones. 升序键顺序视图及其迭代器比降序视图更快。

If you explain more about what features you want from List , I can answer better whether ConcurrentSkipListSet will be able to work. 如果您更多地介绍了List想要的功能,那么我可以更好地回答ConcurrentSkipListSet是否可以工作。


Edit: 编辑:

Ah, I see. 知道了 After some back and forth in the comment, it seems like you need to be able to stick two objects that are equivalent into the Set which isn't possible. 在评论中来回往复后,似乎您需要能够将两个等效的对象粘贴到Set ,这是不可能的。 What we worked out is to never have compareTo(...) return 0. It's a bit of a hack but using AtomicLong to generate a unique number for each object, you can then compare those numbers whenever the real comparison field (in this case a numerical timeout value) is equal. 我们计算出的结果是永远不会使compareTo(...)返回0。这有点AtomicLong ,但是使用AtomicLong为每个对象生成一个唯一的数字,然后您可以在真正的比较字段中对这些数字进行比较(在这种情况下,数字超时值)相等。 This will allow objects with the same field to be inserted into the Set and kept in the proper order based on the field. 这将允许将具有相同字段的对象插入到Set并根据该字段以正确的顺序进行保存。

You can create the Set with a comparator that never returns 0. 您可以使用不返回0的比较器创建Set。

private Set<Obj> entities = new ConcurrentSkipListSet<>((o1, o2) -> {
      if (o1.equals(o2)) {
         // Return -1 or 1 - decide where you want to place an object when it's equals to another one
         return -1;
      }
      // Implement the sorting order below
      if (o1.getTimestamp() < o2.getTimestamp()) {
         return -1;
      }
      if (o1.getTimestamp() > o2.getTimestamp()) {
         return 1;
      }

      return -1;
   })

; ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM