Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collections.synchronizedList vs Vector [duplicate]

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?

like image 274
Murali Avatar asked Jan 07 '14 07:01

Murali


People also ask

Which one is faster ArrayList or Vector?

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.

What is synchronized and non synchronized in Java Collections?

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.

Why ArrayList is non synchronized?

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.


1 Answers

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?

like image 146
Boris Pavlović Avatar answered Oct 06 '22 00:10

Boris Pavlović