How does Vector implement synchronization?

Answer Posted / azad bajaj

Almost all the methods in the Vector class are synchronized.
All the methods which either change (read or write) the
values, or change the size or the change the capacity of
the vector.
example method:

public synchronized void setElementAt(E obj, int index) {
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException
(index + " >= " + elementCount);
}
elementData[index] = obj;
}

or

public synchronized void removeElementAt(int index) {
modCount++;
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException
(index + " >= " +

elementCount);
}
else if (index < 0) {
throw new ArrayIndexOutOfBoundsException
(index);
}
int j = elementCount - index - 1;
if (j > 0) {
System.arraycopy(elementData, index + 1,
elementData, index, j);
}
elementCount--;
elementData[elementCount] = null; /* to let gc
do its work */
}

Is This Answer Correct ?    5 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between processes and threads?

792


What is an iterator java?

763


What is the difference between class & structure?

816


What is void in java?

846


Compare overloading and overriding?

813


What is the difference between super class & sub class?

825


What is meant by polymorphism?

792


When will we use them?

824


What is a class component?

881


What is the purpose of default constructor?

803


What is the difference between checked exception and unchecked exception?

816


What is meant by data hiding in java?

882


What is math floor in java?

697


Tell me how many ways are there to initialise an integer with a constant.

896


What is a numeric literal?

773