Reputation: 69
I know both Array List and Vector class in Java is a bad choice with multi threading. I knew Array List is not synchronised that's why not good with Multi Threading. I want to know the reason behind the poor performance of synchronised vector class in Java with multi threading.
What is the reason behind that?
Upvotes: 1
Views: 269
Reputation: 226
Vector synchronizes access to its data for every method individually.
There may be cases when synchronization is not needed at all but it cannot be removed from vector. For such cases Vector will be inefficient because unnecessary synchronization still has its costs.
On the other hand there may be cases when a whole sequence of method invocations should be synchronized. Then individual synchronization on Vector's methods is useless and again adds performance costs.
ArrayList can be used in multi-threaded programs but with external synchronization.
I would recommend reading Java Concurrency in Prctice to get in depth understanding on the topic. It will help you to get an idea of external/internal synchronization, concurrent collections and a lot more which is necessary to really know the difference between ways of synchronization.
Upvotes: 2