在 Java 中使用 Hashtable、Vector 或 HashMap 或 ArrayList
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/453684/
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
Use Hashtable, Vector or HashMap or ArrayList in Java
提问by Berlin Brown
One meme that gets stressed with Java development is always use ArrayList over Vector. Vector is deprecated. That may be true, but Vector and Hashtable have the advantage that they are synchronized.
一个在 Java 开发中受到压力的模因总是使用 ArrayList 而不是 Vector。不推荐使用向量。这可能是真的,但 Vector 和 Hashtable 的优点是它们是同步的。
I am working with a heavily concurrent oriented application, wouldn't it benefit to use objects that are synchronized like Vector? It seems that they have their place?
我正在使用一个面向大量并发的应用程序,使用像 Vector 一样同步的对象不是有好处吗?似乎有他们的位置?
采纳答案by falstro
The problem with Vector and Hashtable is that they're only locally synchronized. They won't break (as in corrupt data) in a concurrent application, however, due to the local synchronization (e.g. get is synchronized, but only until get returns), you'll be wanting to perform your own synchronization anyway for situations such as iteration over the content. Now, even your put-method needs some extra synchronization to cooperate with the iteration synchronization and you end up with a situation where your Hashtable/Vector is doubly synchronized.
Vector 和 Hashtable 的问题在于它们仅在本地同步。它们不会在并发应用程序中中断(如损坏的数据),但是,由于本地同步(例如 get 已同步,但仅在 get 返回之前),您无论如何都希望在以下情况下执行自己的同步作为内容的迭代。现在,即使你的 put-method 也需要一些额外的同步来配合迭代同步,你最终会遇到你的 Hashtable/Vector 是双重同步的情况。
回答by Paul Tomblin
If you need synchronized ArrayList or HashMap, you can wrap them.
如果你需要同步的 ArrayList 或 HashMap,你可以把它们包装起来。
List list = Collections.synchronizedList(new ArrayList(...));
Map m = Collections.synchronizedMap(new HashMap(...));
Personally I find the "synchronized" methods in these collections not very useful in heavy threaded code. There are some newer collections that help a lot more, but mostly I find myself making my own synchronize objects and synchronizing around them, or using the new locks in java.util.concurrent
我个人发现这些集合中的“同步”方法在重线程代码中不是很有用。有一些较新的集合可以提供更多帮助,但大多数情况下我发现自己创建了自己的同步对象并围绕它们进行同步,或者使用 java.util.concurrent 中的新锁
回答by Joachim Sauer
Synchronization has its place, but that's not the only difference between Vector
and ArrayList
. Vector
grows its internal storage array by a fixed amount every time it exceeds its capacity, while ArrayList
grows it by a fixed factor, which is usually a much better approach (since it gives an amortized costof O(1) for appending an item).
同步有其一席之地,但这并不是Vector
和之间的唯一区别ArrayList
。Vector
每次超过其容量时,将其内部存储阵列增长固定数量,同时ArrayList
按固定因子增长,这通常是一种更好的方法(因为它为附加项目提供了O(1)的摊销成本)。
Also note that Collections.synchronizedList()
can be used to create a synchronized view on any List
implementation, so you don't have to be bound to the characteristics of Vector
(you might want a synchronized LinkedList
for example).
另请注意,Collections.synchronizedList()
可用于在任何List
实现上创建同步视图,因此您不必绑定到特性Vector
(LinkedList
例如,您可能需要同步)。
回答by Douglas Leeder
You can use static Collections methods to convert a List or Map into a synchronized version: http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html#synchronizedList(java.util.List)
您可以使用静态集合方法将列表或映射转换为同步版本:http: //java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html#synchronizedList(java.util 。列表)
In general you normally need to lock for more that an individual call to the list or map.
通常,您通常需要锁定比对列表或地图的单个调用更多的锁定。
回答by Clayton
It seems to me that the only times you'd need the Collection itself to be thread safe are:
在我看来,您需要 Collection 本身是线程安全的唯一时间是:
- If the Collection is visible from outside the class (Public or default scope)
- If you're returning a handle to the collection from a method
- If the collection is a static member of your class
- 如果集合从类外部可见(公共或默认范围)
- 如果您从方法返回集合的句柄
- 如果集合是类的静态成员
All of those are probably a bad ideas design-wise.
所有这些在设计方面都可能是一个糟糕的想法。
A better approach would be to make the collection itself Private or Protected, and access it via synchronized methods. In the case of the static member, if you had a need to do that, a Singleton would be a better way to go.
更好的方法是将集合本身设为私有或受保护,并通过同步方法访问它。在静态成员的情况下,如果您需要这样做,单例将是更好的方法。
回答by Peter ?tibrany
ConcurrentHashMapis way fasterthan Hashtable. It's concurrent, not just synchronized. It admits multiple readers/writers at once.
ConcurrentHashMap的是方式更快比Hashtable的。它是并发的,而不仅仅是同步的。它同时允许多个读者/作者。
There is no such 'concurrent' array list though. Depending on your needs, CopyOnWriteArrayListmay or may not be what you need.
但是没有这样的“并发”数组列表。根据您的需要,CopyOnWriteArrayList可能是也可能不是您需要的。