并发集合队列

编程入门 行业动态 更新时间:2024-10-11 09:26:45
本文介绍了并发集合队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

也许这是一个愚蠢的问题,但我似乎找不到一个明显的答案.

Maybe this is a silly question, but I cannot seem to find an obvious answer.

我需要一个仅包含唯一值的并发 FIFO 队列.尝试添加队列中已经存在的值只会忽略该值.如果不是为了线程安全,那将是微不足道的.是否有 Java 中的数据结构或互联网上的代码片段表现出这种行为?

I need a concurrent FIFO queue that contains only unique values. Attempting to add a value that already exists in the queue simply ignores that value. Which, if not for the thread safety would be trivial. Is there a data structure in Java or maybe a code snipit on the interwebs that exhibits this behavior?

推荐答案

如果你想要比完全同步更好的并发性,我知道有一种方法可以做到这一点,使用 ConcurrentHashMap 作为支持映射.以下仅为草图.

If you want better concurrency than full synchronization, there is one way I know of to do it, using a ConcurrentHashMap as the backing map. The following is a sketch only.

public final class ConcurrentHashSet<E> extends ForwardingSet<E> implements Set<E>, Queue<E> { private enum Dummy { VALUE } private final ConcurrentMap<E, Dummy> map; ConcurrentHashSet(ConcurrentMap<E, Dummy> map) { super(map.keySet()); this.map = Preconditions.checkNotNull(map); } @Override public boolean add(E element) { return map.put(element, Dummy.VALUE) == null; } @Override public boolean addAll(Collection<? extends E> newElements) { // just the standard implementation boolean modified = false; for (E element : newElements) { modified |= add(element); } return modified; } @Override public boolean offer(E element) { return add(element); } @Override public E remove() { E polled = poll(); if (polled == null) { throw new NoSuchElementException(); } return polled; } @Override public E poll() { for (E element : this) { // Not convinced that removing via iterator is viable (check this?) if (map.remove(element) != null) { return element; } } return null; } @Override public E element() { return iterator().next(); } @Override public E peek() { Iterator<E> iterator = iterator(); return iterator.hasNext() ? iterator.next() : null; } }

采用这种方法,一切都不是阳光.除了使用支持映射的 entrySet().iterator().next() 之外,我们没有合适的方法来选择头部元素,结果是随着时间的推移映射变得越来越不平衡.由于更大的桶冲突和更大的段争用,这种不平衡是一个问题.

All is not sunshine with this approach. We have no decent way to select a head element other than using the backing map's entrySet().iterator().next(), the result being that the map gets more and more unbalanced as time goes on. This unbalancing is a problem both due to greater bucket collisions and greater segment contention.

注意:此代码在一些地方使用了 Guava.

Note: this code uses Guava in a few places.

更多推荐

并发集合队列

本文发布于:2023-11-26 09:17:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633530.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:队列

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!