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
Which sorting algorithm is in place?
What are different types of states exist for a thread?
Write a function to find out longest palindrome in a given string?
What is file in java?
What is difference between core java and java ee?
What is string pool in java?
What is arguments in java?
What will happen when using pass by reference in java?
Give an example of use of pointers in java class.
Is an integer an object?
What do the thread?class methods run() and start() do?
Can we override singleton class?
What does the exclamation mark mean in java?
What is the difference between JDK and JVM?
How can we make sure main() is the last thread to finish in java program?