在 Java 中使用 Hashtable、Vector 或 HashMap 或 ArrayList [英] Use Hashtable, Vector or HashMap or ArrayList in Java

查看:33
本文介绍了在 Java 中使用 Hashtable、Vector 或 HashMap 或 ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个在 Java 开发中受到压力的模因总是使用 ArrayList 而不是 Vector.不推荐使用向量.这可能是真的,但 Vector 和 Hashtable 的优势在于它们是同步的.

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.

我正在处理一个面向大量并发的应用程序,使用像 Vector 一样同步的对象不是有好处吗?好像有他们的位置?

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 和 Hashtable 的问题在于它们仅在本地同步.它们不会在并发应用程序中中断(如损坏的数据),但是,由于本地同步(例如 get 已同步,但仅在 get 返回之前),您无论如何都希望在以下情况下执行自己的同步作为内容的迭代.现在,即使你的 put-method 也需要一些额外的同步来配合迭代同步,你最终会遇到你的 Hashtable/Vector 双重同步的情况.

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.

这篇关于在 Java 中使用 Hashtable、Vector 或 HashMap 或 ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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