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

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

问题描述

一个在Java开发中受到压力的模因总是在Vector上使用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是同步的,但仅在获得返回之前),您将希望无论如何都要执行自己的同步作为对内容的迭代。现在,即使你的put方法需要一些额外的同步来配合迭代同步,你最终会遇到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天全站免登陆