最好的Java线程安全锁定机制的集合? [英] Best Java thread-safe locking mechanism for collections?

查看:133
本文介绍了最好的Java线程安全锁定机制的集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中控制对集合的多个访问的最缓慢的线程安全机制是什么?

What would be the least-slow thread-safe mechanism for controlling multiple accesses to a collection in Java?

我将对象添加到集合的顶部,我很不确定什么是最好的表演的集合。它是一个向量还是一个队列?我最初认为一个ArrayList会很快,但我运行一些实验,这是非常缓慢。

I am adding objects to the top of a collection and i am very unsure what would be the best performing collection. Would it be a vector or a queue? I originally thought an ArrayList would be fast but i ran some experiments and it was very slow.

编辑:在我的插入测试使用volatile的矢量delared似乎是最快的?

In my insertion testing a Vector delared using volatile seems to be the fastest?

推荐答案

所使用的确切操作应该决定使用什么。但是,由于容器很大程度上是一种抽象类型,所以写成它可以工作 - 可靠 - 然后配置文件,确保功能需求,根据需要进行优化blah blah:)

The exact operations used should determine what is used. However, since the container is largely an abstract type, write it so it works -- reliably -- then profile, ensure functional requirements, optimize as needed, blah blah :)

通常,我主要使用并发集合是用于在线程之间传输对象的队列。在这种情况下,我从 ConcurrentLinkedQueue 开始如果没有其他原因喜欢无锁算法(这不意味着它会更快,虽然: - )。

Normally, my main use for a "concurrent collection" is a Queue that is used to transfer objects between threads. In this case I start with ConcurrentLinkedQueue if for no other reason than liking the "lock-free" algorithm (this doesn't mean it will be faster though :-).

通常队列和/或LinkedList是一个很好的数据结构,附加到结尾。根据包括特定使用模式的情况,包括:线程争用,移除的项目数,项目如何移除等等,除了开始/结束之外的所有的快速杀死可以使用清除(AbstractQueue的一部分)和项重新添加 - ConcurrentLinkedQueue允许头尾检查/操作。但是,我会敦促保持简单,写一个特定的接口合同和只使用当前的方法,直到有强有力的证据表明不满足功能需求。

Generally a Queue and/or LinkedList is a good data-structure to append to the end. Depending upon the circumstances including specific usage patterns including: thread contention, number of items removed, how items are removed, etc, a "fast-kill" of all but the start/end might be accomplished faster with a clear (part of AbstractQueue) and item re-addition -- ConcurrentLinkedQueue allows both head and tail inspection/manipulation. However, I would urge "keeping it simpler", writing to a "specific interface contract" and "just use the current approach" until there is strong evidence that a functional-requirement is not met.

就性能而言,它实际上取决于特定的用例/操作数据结构特性和数据结构实现。 ArrayList在某些情况下可能会比Vector慢,并且确实记录在案。如果这个性能产生的差异,一些听起来鱼腥。文章位于 Java最佳做法 - Vector vs ArrayList vs HashSet 包含一个不错的读。注意注释。

As far as the performance, it really depends upon the particular use-case/operations data-structure characteristics and data-structure implementation. ArrayList may very will be slower than Vector in some cases, and is indeed documented to be. If this performance makes the difference though, something sounds fishy. The article at Java Best Practices – Vector vs ArrayList vs HashSet contains a nice read. Pay attention to the comments as well.

编辑:请记住,并发数据结构通常只意味着一个操作(方法调用)是原子的(并且可能不是所有的操作在奇怪的情况!)。仍然可能需要实现大范围的同步或其他方法以实现所需的线程安全级别。也就是说, h.putIfAbsent(k,v)(来自ConcurrentHashMap)与不同 if !h.containsKey(k)){h.put(k,v); } - 作为一个案例,这样的问题适用于上述清除然后添加方法。

Remember that a "concurrent" data-structure generally only implies that a single operation (method call) is atomic (and perhaps not all operations in odd cases!). It may still be required to implement a large-scoped synchronization or other approach to achieve the required level of thread-safety. That is, a h.putIfAbsent(k,v) (from ConcurrentHashMap) is not the same as a if (!h.containsKey(k)) { h.put(k, v); } -- as a case-in-point, an issue like this applies to the "clear then add" approach mentioned above.

编码。

这篇关于最好的Java线程安全锁定机制的集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆