Java 向量中的同步

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

Synchronization in Vectors in Java

javasynchronizationvector

提问by emkrish

what is meant by vector in Java is thread safe and synchronized, how is it made thread safe. I'm looking at internal details of implementation

Java中的vector是什么意思是线程安全和同步的,它是如何使线程安全的。我正在查看实施的内部细节

回答by notnoop

The methods are marked as synchronized. Check out the Synchronized Method tutorial.

这些方法被标记为synchronized。查看同步方法教程

回答by akf

Vectoris considered 'thread-safe' because access the the internals of the Vector is synchronized. Methods such as add(), get(), size(), etc, are all synchronized such that modifications to the internal structure of the Vectorand access to that internal structure cannot be processed simultaneously by separate threads.

Vector被认为是“线程安全的”,因为对 Vector 内部的访问是同步的。方法,例如add()get()size(),等等,都被同步,使得修改的内部结构Vector和访问该内部结构不能同时由单独的线程处理。

回答by Thilo

It is made "thread-safe" by merit of all its methods being synchronized (via the synchronized keyword), see the OpenJDK source code.

它的所有方法都是同步的(通过 synchronized 关键字),因此它是“线程安全的”,请参阅OpenJDK 源代码

What the synchronized keyword does is that it prevents more than one thread from executing any of the synchronized methods at the same time. It is using a lock internally, that a thread has to obtain when entering of of those methods, and that the thread releases when it leaves the method.

synchronized 关键字的作用是防止多个线程同时执行任何同步方法。它在内部使用锁,线程在进入这些方法时必须获得该锁,并且在离开该方法时线程释放。

Note that this does not really help much, because while it prevents inconsistent internal state of the vector, this does in no way guarantee a level of consistency on a higher level (a useful level for an application).

请注意,这并没有多大帮助,因为虽然它可以防止向量的内部状态不一致,但这并不能保证更高级别(对应用程序有用的级别)的一致性级别。

Consider this example that shows that you still need to use synchronization in your application code (so that you might just as well have used the unsynchronized ArrayList):

考虑这个示例,它表明您仍然需要在应用程序代码中使用同步(这样您也可以使用未同步的 ArrayList):

 // BROKEN CODE, needs external synchronization
 // only add an element if the vector is empty
 if(vector.isEmpty())
     vector.add(anElement);

回答by user1643635

Please find the below excerpts from java api

请从java api中找到以下摘录

First, it is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.

首先,对同一对象的同步方法的两次调用不可能交错。当一个线程正在为一个对象执行同步方法时,所有其他调用同一对象的同步方法的线程都会阻塞(挂起执行),直到第一个线程完成对对象的处理。

Second, when a synchronized method exits, it automatically establishes a happens-before relationship with any subsequent invocation of a synchronized method for the same object. This guarantees that changes to the state of the object are visible to all threads.

其次,当一个同步方法退出时,它会自动建立一个发生在同一个对象的同步方法的任何后续调用之前的关系。这保证了对象状态的更改对所有线程都是可见的。