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

how we can make a read-only class in java?

537


What is singleton class example?

582


Explain about OOPS concepts and fundamentals.

589


How use .contains in java?

614


What is java util collection?

552






What is the purpose of nested class in java?

593


What is definition and declaration?

518


When will you define a method as static in Java?

560


What is a parameter in a function?

524


In java, how we can disallow serialization of variables?

547


Describe the term diamond problem.

572


What is an empirical question?

535


What does bitwise or mean?

563


How do you use wildcards?

528


What is bigger kb or mb?

577