问题描述
我试图理解为什么必须同步 Java 中的向量.无论我通过谷歌到达哪里,他们都说它是同步的,因为它的元素是同步的,当一个线程正在访问它时会阻止其他线程访问.但我真正想知道的是为什么必须这样做? Java 应该将它留给访问它的人.但是为什么 Java 强制所有访问 vector 的人都使用同步的数据结构呢?我同意那些说 ArrayList 可以用于我的要求的人.但我的观点是 vector 会给我什么??
I was trying to understand why vector in Java has to be synchronized. Wherever I reached through google, they said that its synchronized because its elements are synchronized blocks other threads to access when one thread is accessing it. But what I really want to know is why it has to be? Java should have left it to the people who are accessing it. But why Java forced all the people accessing vector to use a synchronized data structure? I agree with people who says that ArrayList can be used for my requirement. But my point is what is that vector is going to give me??
谢谢和问候,Rengasami Ramanujam
Thanks and regards,Rengasami Ramanujam
推荐答案
历史.
Vector
是 JDK 1.0 的一部分;ArrayList
不是.我们了解到,同步作为默认意味着性能不佳,因此当 Java Collections API 出现时,它被逆转了.
Vector
was part of JDK 1.0; ArrayList
was not. We learned that synchronization as a default meant poor performance, so it was reversed when the Java Collections API came out.
您仍然可以选择使用 java.util.Collections
中的方法同步您需要的任何集合,但这是您的选择.
You can still choose to synchronize any collection you need to using methods in java.util.Collections
, but it's your choice.
您还应该了解作为 java.util.concurrent
包一部分的集合.集合的同时访问当然需要精心编排.
You should also be aware of the collections that are part of the java.util.concurrent
packages. Simultaneous access of collections certainly needs to be choreographed carefully.
这篇关于为什么Java中的vector必须同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!