src/share/classes/java/util/Vector.java

Print this page
rev 9756 : 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
Reviewed-by: mduigou, martin, psandoz
Contributed-by: Sandhya.Viswanathan@intel.com, Mike.Duigou@oracle.com

@@ -551,11 +551,10 @@
      * @param      index   the index of the object to remove
      * @throws ArrayIndexOutOfBoundsException if the index is out of range
      *         ({@code index < 0 || index >= size()})
      */
     public synchronized void removeElementAt(int index) {
-        modCount++;
         if (index >= elementCount) {
             throw new ArrayIndexOutOfBoundsException(index + " >= " +
                                                      elementCount);
         }
         else if (index < 0) {

@@ -563,10 +562,11 @@
         }
         int j = elementCount - index - 1;
         if (j > 0) {
             System.arraycopy(elementData, index + 1, elementData, index, j);
         }
+        modCount++;
         elementCount--;
         elementData[elementCount] = null; /* to let gc do its work */
     }
 
     /**

@@ -591,18 +591,18 @@
      * @param      index   where to insert the new component
      * @throws ArrayIndexOutOfBoundsException if the index is out of range
      *         ({@code index < 0 || index > size()})
      */
     public synchronized void insertElementAt(E obj, int index) {
-        modCount++;
         if (index > elementCount) {
             throw new ArrayIndexOutOfBoundsException(index
                                                      + " > " + elementCount);
         }
         ensureCapacityHelper(elementCount + 1);
         System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
         elementData[index] = obj;
+        modCount++;
         elementCount++;
     }
 
     /**
      * Adds the specified component to the end of this vector,

@@ -614,12 +614,12 @@
      * method (which is part of the {@link List} interface).
      *
      * @param   obj   the component to be added
      */
     public synchronized void addElement(E obj) {
-        modCount++;
         ensureCapacityHelper(elementCount + 1);
+        modCount++;
         elementData[elementCount++] = obj;
     }
 
     /**
      * Removes the first (lowest-indexed) occurrence of the argument

@@ -651,15 +651,15 @@
      *
      * <p>This method is identical in functionality to the {@link #clear}
      * method (which is part of the {@link List} interface).
      */
     public synchronized void removeAllElements() {
-        modCount++;
         // Let gc do its work
         for (int i = 0; i < elementCount; i++)
             elementData[i] = null;
 
+        modCount++;
         elementCount = 0;
     }
 
     /**
      * Returns a clone of this vector. The copy will contain a

@@ -776,12 +776,12 @@
      * @param e element to be appended to this Vector
      * @return {@code true} (as specified by {@link Collection#add})
      * @since 1.2
      */
     public synchronized boolean add(E e) {
-        modCount++;
         ensureCapacityHelper(elementCount + 1);
+        modCount++;
         elementData[elementCount++] = e;
         return true;
     }
 
     /**

@@ -877,17 +877,19 @@
      * @param c elements to be inserted into this Vector
      * @return {@code true} if this Vector changed as a result of the call
      * @throws NullPointerException if the specified collection is null
      * @since 1.2
      */
-    public synchronized boolean addAll(Collection<? extends E> c) {
-        modCount++;
+    public  boolean addAll(Collection<? extends E> c) {
         Object[] a = c.toArray();
         int numNew = a.length;
+        synchronized (this) {
         ensureCapacityHelper(elementCount + numNew);
         System.arraycopy(a, 0, elementData, elementCount, numNew);
+            modCount++;
         elementCount += numNew;
+        }
         return numNew != 0;
     }
 
     /**
      * Removes from this Vector all of its elements that are contained in the

@@ -948,26 +950,28 @@
      * @throws ArrayIndexOutOfBoundsException if the index is out of range
      *         ({@code index < 0 || index > size()})
      * @throws NullPointerException if the specified collection is null
      * @since 1.2
      */
-    public synchronized boolean addAll(int index, Collection<? extends E> c) {
-        modCount++;
+    public boolean addAll(int index, Collection<? extends E> c) {
         if (index < 0 || index > elementCount)
             throw new ArrayIndexOutOfBoundsException(index);
 
         Object[] a = c.toArray();
         int numNew = a.length;
+        synchronized(this) {
         ensureCapacityHelper(elementCount + numNew);
 
         int numMoved = elementCount - index;
         if (numMoved > 0)
             System.arraycopy(elementData, index, elementData, index + numNew,
                              numMoved);
 
         System.arraycopy(a, 0, elementData, index, numNew);
+            modCount++;
         elementCount += numNew;
+        }
         return numNew != 0;
     }
 
     /**
      * Compares the specified Object with this Vector for equality.  Returns

@@ -1045,16 +1049,16 @@
      * Shifts any succeeding elements to the left (reduces their index).
      * This call shortens the list by {@code (toIndex - fromIndex)} elements.
      * (If {@code toIndex==fromIndex}, this operation has no effect.)
      */
     protected synchronized void removeRange(int fromIndex, int toIndex) {
-        modCount++;
         int numMoved = elementCount - toIndex;
         System.arraycopy(elementData, toIndex, elementData, fromIndex,
                          numMoved);
 
         // Let gc do its work
+        modCount++;
         int newElementCount = elementCount - (toIndex-fromIndex);
         while (elementCount != newElementCount)
             elementData[--elementCount] = null;
     }