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 is default size of arraylist in java?
Explain about strings in java?
What is the importance of static variable?
How many bits are in a sentence?
What is size () in java?
What is the function of java?
Can we create an object of private class?
What is linkedlist in java?
What are selection structures?
what are different ways in which a thread can enter the waiting state? : Java thread
What is meant by main method?
What is sizeof () operator?
Can you instantiate the math class in Java?
Why are the objects immutable in java?
What is immutable state?