Which one is Better Vector or SynchronizedList

Vector and SynchronizedList are synchronized.

In case of vector the synchronization is at method level,

Example:

public synchronized E get(int index) {
return (E)elementData[index];
}

And in case of SynchronizedList, the statements within the methods are synchronized on the list object.
SynchronizedList is static inner class inside collections class which implements list interface and necessary methods using synchronized blocks like below.

Link to Source code

Example :

public E get(int index) {
synchronized(colobj) {return list.get(index);}
}
where colobj is the collection object.

When you do Collections.synchronizedList(new ArrayList()); you end up creating 2 instances of List. Whereas, in case of Vector, you are creating only one instance.



Popular posts from this blog

What's New in JAVA 8

Singleton Design Pattern

Internals of JSF