Vector - Thread Safe

Is Vector Thread Safe?

Yes it is thread safe because all the method are ATOMIC. Because of which the underlying array cannot be corrupted. But composite operations in a vector would not be thread safe in case you do not provide thread-safety (e.g. Synchronized keyword, etc).

Consider the below example:

vector.get(vector.size()-1);

This example seems to be thread-safe. But there could be some other thread manipulating the vector (e.g. delete elements) just after

vector.size()

but before

vector.get()

Similarly, the enumerator may provide unexpected results if the underlying vector is modified while iterating. Iterator in that case would throw exception (because of fail-fast) instead of not telling anything.