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
What are the types of arrays in java?
What happens if an exception is throws from an object's destructor?
Tell us something about set interface.
Write a program to check string is palindrome without using loop?
What is a modifier?
What is Java Package and which package is imported by default?
In a container there are 5 components. I want to display the all the components names, how will you do that one?
What is unicode datatype?
What are the disadvantages of object oriented programming?
What is default size of arraylist in java?
Is space a character in java?
What is udp in java?
What is java lang object?
Explain the difference between the public, private, final, protected, and default modifiers?
What is the difference between size and length in java?