I would like to use collection classes for adding,removing and retrieving objects in multiple thread.
Collections.synchronizedList
and Vector
both classes are thread safe. Does any tell me the difference between Collections.synchronizedList
and Vector
and explain that when should I use Vector
and Collections.synchronizedList
?
Performance: ArrayList is faster. Since it is non-synchronized, while vector operations give slower performance since they are synchronized (thread-safe), if one thread works on a vector, it has acquired a lock on it, which forces any other thread wanting to work on it to have to wait until the lock is released.
Non synchronized -It is not-thread safe and can't be shared between many threads without proper synchronization code. While, Synchronized- It is thread-safe and can be shared with many threads.
ArrayList is non synchronized because if ArrayList is synchronized then only one thread can work on ArrayList at a time and rest of all threads cannot perform other operations on the ArrayList until the first thread release the lock. This causes overhead and reduces performance. This applies for all collections.
Here are few words on why Vector is an obsolete/deprecated class: Why is Java Vector class considered obsolete or deprecated?
Generally about their difference: In java, Vector and Collections.synchronizedList are all synchronized, what's the difference?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With