views:

42

answers:

2

All,

The edge Vector class has over ArrayList is that it is synchronized and hence ensures thread safety. However, between CopyOnWriteArray and Vector, what should be the preferred considering thread safety and performance in consideration.

+1  A: 

It depends on the usage pattern - if you have much more reads than writes, use CopyOnWriteArrayList, otherwise use Vector.

Vector introduces a small synchronization delay for each operation, when CopyOnWriteArrayList has a longer delay for write (due to copying) but no delay for reads.

Another consideration is a behaviour of iterators - Vector requires explicit synchronization when you are iterating it (so write operations can't be executed at the same time), CopyOnWriteArrayList doesn't.

axtavt
+1  A: 

Overall, it depends on the frequency and nature of read and write operations, and the size of the array.

You'll need to benchmark in your context to be sure, but here are some general principles:

  • If you are only going to read the array, then even ArrayList is thread safe (since the only non-thread-safe modifications are those that modify the list). Hence you would want to use a non-synchronised data structure, either ArrayList or CopyOnWriteArrayList would probably work equally well.
  • If reads are much more common compared to writes then you would tend to prefer CopyOnWriteArrayList, since the array copying overhead is only incurred on writes.
  • If the size of the Array is small, then the cost of making array copies will also be small, hence this will favour CopyOnWriteArrayList over Vector.

You may also want to consider two other options:

  • Use an ArrayList but put the synchronisation elsewhere to ensure thread safety. This is actually the method I personally use most often - basically the idea is to use a separate, higher-level lock to protect all the relevant data structures at the same time. This is much more efficient than having synchronisation on every single operation as Vector does.
  • Consider an immutable persistent data structure - these are guaranteed to be thread safe due to immutability, do not require synchronisation and also benefit from low overhead (i.e. they share most data between different instances rather than making complete new copies). Languages like Clojure use these to get ArrayList-like performance while also guaranteeing full thread safety.
mikera
Also, consider using Collections.synchronizedList to wrap a non-thread-safe List, instead of doing external synchronizations
barjak