Java 向量线程安全

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23246059/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:29:49  来源:igfitidea点击:

Java Vector Thread safety

javavectorthread-safety

提问by Smarty77

Is there any danger, if im using one Vector(java.util.Vector) on my server program when im accessing it from multiple threads only for reading? (myvector .size() .get() ...) For writing im using synchronized methods. Thank you.

如果我在我的服务器程序上使用一个 Vector(java.util.Vector) 时我从多个线程访问它只是为了阅读,是否有任何危险?(myvector .size() .get() ...) 用于使用同步方法编写 im。谢谢你。

采纳答案by Denis Kulagin

Vectoris a thread-safe collection - all it's methods are syncronized. This is why it's recommended to use ArrayListinstead - it's not thread-safe which results in a better performance for single-thread applications.

Vector是一个线程安全的集合——它的所有方法都是同步的。这就是为什么建议使用ArrayList代替 - 它不是线程安全的,这会为单线程应用程序带来更好的性能。

回答by Ralf

The java.util.Vector methods are all synchronized. So using it from multiple threads is "safe". You only need to synchronize if you need a read-evaluate-write process to be atomic. Synchronizing your own methods does not necessarily make your code thread-safe for those scenarios. If the shared state is the Vector object, then you need to synchronize on the instance of your Vector, not on an instance of your own classes.

java.util.Vector 方法都是同步的。所以从多个线程使用它是“安全的”。如果您需要一个读-评估-写过程是原子的,您只需要同步。同步您自己的方法并不一定会使您的代码在这些场景中成为线程安全的。如果共享状态是 Vector 对象,那么您需要在 Vector 的实例上同步,而不是在您自己的类的实例上。

回答by Jakub H

Vector class is thread safe (size, get, set methods are synchronized), but if you want to achieve better performance you should try to use CopyOnWriteArrayListor Collections.synchronizedListinstead.

Vector 类是线程安全的(size、get、set 方法是同步的),但是如果您想获得更好的性能,您应该尝试使用CopyOnWriteArrayListCollections.synchronizedList代替。

回答by Alexey Malev

As stated above, every single method of Vectoris thread-safe by its own because of synchronizedmodifiers. But, if you need some complex operations, such as get()or add()based on condition which is related to the same vector, this is not thread-safe. See example below:

如上所述,Vector由于synchronized修饰符,每个方法都是线程安全的。但是,如果您需要一些复杂的操作,例如get()add()基于与相同 相关的条件,则vector这不是线程安全的。请参阅下面的示例:

if (vector.size() > 0) {
    System.out.println(vector.get(0));
}

This code has a race condition between size()and get()- the size of vector might be changed by other thread after our thread verified the vector is not empty, and thus get()call may return unexpected results. To avoid this, the sample above should be changed like this:

此代码在size()和之间存在竞争条件get()- 在我们的线程验证向量不为空后,向量的大小可能会被其他线程更改,因此get()调用可能会返回意外结果。为了避免这种情况,上面的示例应该像这样更改:

synchronized (vector) {
    if (vector.size() > 0) {
        System.out.println(vector.get(0));
    }
}

Now this "get-if-not-empty" operation is atomic and race condition-free.

现在这个“get-if-not-empty”操作是原子的并且没有竞争条件。