6

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?

axtavt
  • 239,438
  • 41
  • 511
  • 482
Redwood
  • 66,744
  • 41
  • 126
  • 187
  • 1
    note that although individual add/remove operations are synchronized, you can't iterate through a vector while other threads are adding/removing (without adding your own locking) see http://stackoverflow.com/questions/1386275/why-java-vector-class-is-considered-obsolete-or-deprecated – David Gelhar Oct 12 '10 at 00:00

1 Answers1

7

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • As long as it is implemented in a thread-safe manner at present I can accept for my purposes that the statement in the Javadoc is somewhat vague. – Redwood Oct 11 '10 at 23:49