Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is java.util.Vector serialization thread-safe?

I know the Vector class is thread-safe for adding and removing elements [reference].

If I serialize a Vector using an ObjectOutputStream am I guaranteed a consistent (and non-corrupt) state when I deserialize it even if other threads are adding and removing objects during the seralization?

like image 640
Lawrence Johnston Avatar asked Oct 11 '10 23:10

Lawrence Johnston


1 Answers

The writeObject() method is synchronized. But there's nothing in the Javadoc that guarantees that unless it's implied by the statement 'Vector is synchronized'.

Note that the readObject() method doesn't need to be synchronized, as the object isn't accessible to anybody until readObject() returns.

like image 146
user207421 Avatar answered Oct 05 '22 23:10

user207421