Recently one of my colleague was asked the same question in Deloitte interview. So you could also be asked the same in your next interview.
Lets come to the point.
All the synchronized collection classes are stated below:
Due to synchronized nature, it does not allow multiple threads to access and modify its resources concurrently. If one thread has access to the vector then other thread wanting to access and operate on it must wait for its turn until the vector is released by the thread in action.
To read about Vector in details. please follow the link Vector in java
Hashtable extends Dictionary class and implements Map interface. Hashtable stores elements in key-value pair.
Hashtable is synchronized in nature.
Due to synchronized nature, it does not allow multiple threads to access and modify itself concurrently.
Note: Both vector and hashtable are legacy classes and are not recommended for use.
CopyOnWriteArrayList is a thread-safe variant of ArrayList which was included in java version 1.5 or java 5.
Here thread-safe indicates that this collection class is synchronized.
Advantage of synchronization:
If you don’t want any resource to be shared simultaneously by multiple threads then mark the resource Synchronized. This makes sure that only one thread can access the resource at one instance and others has to wait for their turn in the queue.
Disadvantage of synchronization:
Because of being synchronized in nature these classes are slow in terms of performance. Reason is simple. Because of synchronization only single thread is able to access the class resource at a time. Other thread has to wait in queue till the first thread completes its task on the resource. Thus multitasking is not possible which in turn impacts speed.
You may also like to read:
- ArrayList vs LinkedList
- ArrayList vs Vector
- Collection vs Collections
- ArrayList vs HashSet
- Iterator vs Enumeration