Vector 应该被弃用吗?

发布于 2024-10-09 05:33:34 字数 116 浏览 6 评论 0原文

当我们不需要同步时,ArrayList比Vector更快。当我们确实需要同步集合时,我们最好使用同步包装器(如果我错了,请纠正我),或者仅在对该集合进行调用时同步代码。在某些情况下,使用 Vector 是否是最佳选择?

When we don't need synchronization ArrayList is faster than Vector. And when we do need a synchronized collection we're better off using Synchronization wrappers (correct me if I'm wrong), or synchronizing the code only when there are calls on that collection. Are there cases where using Vector is the best option?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一袭水袖舞倾城 2024-10-16 05:33:35

虽然在新代码中使用 Vector 很少是一个好主意,但没有迫切需要弃用。虽然新的集合类非常出色,但使用 Vector 类实际上不会破坏任何内容。

此外,还有许多其他标准 Java API 依赖于 Vector API,毫无疑问还有数十万个客户和第三方应用程序也在使用它。

基本上,弃用 Vector 会造成不必要的破坏。 没有必要强迫人们去改变可靠工作的代码,即使速度会稍微慢一些。


有人建议/暗示他们可以弃用 Vector 而无需(永远)实际删除它。但问题是弃用警告为真实的人创造了真正的工作。如果不必要地这样做,忙碌的人将开始默认抑制警告。 (回想一下那个男孩喊狼来了的故事......)

While it is rarely a good idea to use Vector in new code, there is no pressing need to deprecate. While the new collections classes are superior, using the Vector class doesn't actually break anything.

Furthermore, there are a number of other standard Java APIs that depend on the Vector API, and there are doubtless hundreds of thousands of customer and third party applications that use it as well.

Basically, deprecating Vector would be unnecessarily disruptive. There is no need to push people into changing code that works reliably, if marginally slower.


It has been suggested / implied that they could deprecate Vector without (ever) actually removing it. But the problem is that deprecation warnings create real work for real people. If this is done unnecessarily, busy people will start suppressing the warnings by default. (Recall the story of the boy who cried wolf .... )

深空失忆 2024-10-16 05:33:35

据我所知,使用 Vector 只是为了拥有依赖于它的核心 API 或 1.2 之前的运行时环境。这或许也是一件令人安慰的事情。有些人只是喜欢向量,因为那是他们最初使用的。

更新:Vector 现已弃用,如此处所述

As far as I can tell, using Vector is only for having a core API that depends on it or a pre-1.2 runtime environment. It might also be a comfort thing. Some people just like Vectors because that's what they originally used.

update: Vector is now deprecated as described here

春风十里 2024-10-16 05:33:35

因为有很多程序都使用 Vector。从 jre 中删除 Vector 意味着它们都会被破坏。另一方面,根据您的需要使用 ArrayList 或其他捆绑的 List 实现之一。

Because there are a lot of programs out there that use Vector. Removing Vector from the jre would mean they would all be broken. ON the other hand use ArrayList or one of the other bundled List implementations instead according to your need.

绝情姑娘 2024-10-16 05:33:35

检查 Java 中的 Lock Elision,现代 JVM 实现(Mustang)足够智能,可以优化共享和共享。非共享对象访问。
Mustang 中的同步优化

所以,这并不重要(向量/带有同步关键字的数组列表);但贬值会导致编译时出现太多警告:(

Check Lock Elision in java, modern JVM implementations (Mustang) are intelligent enough to optimize shared & non shared object access.
Synchronization optimizations in Mustang

So, it does not really matters (vector / arraylist with synchronized keyword); but depricating will result in too many warnings on compilation :(

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文