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 arraylist e in java?
What is an immutable class?
What is externalizable?
What does super keyword do?
Is string is a class in java?
How can we run a java program without making any object?
What does it mean that a method or field is “static”?
Can a serialized object be transferred via network?
What is a lightweight component?
What is the use of math abs in java?
Are arrays classes in java?
What is meant by local variable and instance variable?
What is consumer in java?
How do you break a loop?
Explain the reason behind ending a program with a system.exit(0)?