src/share/classes/java/util/concurrent/atomic/AtomicLongArray.java

Print this page




 127     /**
 128      * Eventually sets the element at position {@code i} to the given value.
 129      *
 130      * @param i the index
 131      * @param newValue the new value
 132      * @since 1.6
 133      */
 134     public final void lazySet(int i, long newValue) {
 135         unsafe.putOrderedLong(array, checkedByteOffset(i), newValue);
 136     }
 137 
 138     /**
 139      * Atomically sets the element at position {@code i} to the given value
 140      * and returns the old value.
 141      *
 142      * @param i the index
 143      * @param newValue the new value
 144      * @return the previous value
 145      */
 146     public final long getAndSet(int i, long newValue) {
 147         long offset = checkedByteOffset(i);
 148         while (true) {
 149             long current = getRaw(offset);
 150             if (compareAndSetRaw(offset, current, newValue))
 151                 return current;
 152         }
 153     }
 154 
 155     /**
 156      * Atomically sets the element at position {@code i} to the given
 157      * updated value if the current value {@code ==} the expected value.
 158      *
 159      * @param i the index
 160      * @param expect the expected value
 161      * @param update the new value
 162      * @return true if successful. False return indicates that
 163      * the actual value was not equal to the expected value.
 164      */
 165     public final boolean compareAndSet(int i, long expect, long update) {
 166         return compareAndSetRaw(checkedByteOffset(i), expect, update);
 167     }
 168 
 169     private boolean compareAndSetRaw(long offset, long expect, long update) {
 170         return unsafe.compareAndSwapLong(array, offset, expect, update);
 171     }
 172 
 173     /**
 174      * Atomically sets the element at position {@code i} to the given
 175      * updated value if the current value {@code ==} the expected value.
 176      *
 177      * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
 178      * and does not provide ordering guarantees, so is only rarely an
 179      * appropriate alternative to {@code compareAndSet}.
 180      *
 181      * @param i the index
 182      * @param expect the expected value
 183      * @param update the new value
 184      * @return true if successful.
 185      */
 186     public final boolean weakCompareAndSet(int i, long expect, long update) {
 187         return compareAndSet(i, expect, update);
 188     }
 189 
 190     /**
 191      * Atomically increments by one the element at index {@code i}.
 192      *
 193      * @param i the index
 194      * @return the previous value
 195      */
 196     public final long getAndIncrement(int i) {
 197         return getAndAdd(i, 1);
 198     }
 199 
 200     /**
 201      * Atomically decrements by one the element at index {@code i}.
 202      *
 203      * @param i the index
 204      * @return the previous value
 205      */
 206     public final long getAndDecrement(int i) {
 207         return getAndAdd(i, -1);
 208     }
 209 
 210     /**
 211      * Atomically adds the given value to the element at index {@code i}.
 212      *
 213      * @param i the index
 214      * @param delta the value to add
 215      * @return the previous value
 216      */
 217     public final long getAndAdd(int i, long delta) {
 218         long offset = checkedByteOffset(i);
 219         while (true) {
 220             long current = getRaw(offset);
 221             if (compareAndSetRaw(offset, current, current + delta))
 222                 return current;
 223         }
 224     }
 225 
 226     /**
 227      * Atomically increments by one the element at index {@code i}.
 228      *
 229      * @param i the index
 230      * @return the updated value
 231      */
 232     public final long incrementAndGet(int i) {
 233         return addAndGet(i, 1);
 234     }
 235 
 236     /**
 237      * Atomically decrements by one the element at index {@code i}.
 238      *
 239      * @param i the index
 240      * @return the updated value
 241      */
 242     public final long decrementAndGet(int i) {
 243         return addAndGet(i, -1);
 244     }
 245 
 246     /**
 247      * Atomically adds the given value to the element at index {@code i}.
 248      *
 249      * @param i the index
 250      * @param delta the value to add
 251      * @return the updated value
 252      */
 253     public long addAndGet(int i, long delta) {
 254         long offset = checkedByteOffset(i);
 255         while (true) {
 256             long current = getRaw(offset);
 257             long next = current + delta;
 258             if (compareAndSetRaw(offset, current, next))
 259                 return next;
 260         }
 261     }
 262 
 263     /**
 264      * Returns the String representation of the current values of array.
 265      * @return the String representation of the current values of array
 266      */
 267     public String toString() {
 268         int iMax = array.length - 1;
 269         if (iMax == -1)
 270             return "[]";
 271 
 272         StringBuilder b = new StringBuilder();
 273         b.append('[');
 274         for (int i = 0; ; i++) {
 275             b.append(getRaw(byteOffset(i)));
 276             if (i == iMax)
 277                 return b.append(']').toString();
 278             b.append(',').append(' ');
 279         }
 280     }
 281 


 127     /**
 128      * Eventually sets the element at position {@code i} to the given value.
 129      *
 130      * @param i the index
 131      * @param newValue the new value
 132      * @since 1.6
 133      */
 134     public final void lazySet(int i, long newValue) {
 135         unsafe.putOrderedLong(array, checkedByteOffset(i), newValue);
 136     }
 137 
 138     /**
 139      * Atomically sets the element at position {@code i} to the given value
 140      * and returns the old value.
 141      *
 142      * @param i the index
 143      * @param newValue the new value
 144      * @return the previous value
 145      */
 146     public final long getAndSet(int i, long newValue) {
 147         return unsafe.getAndSetLong(array, checkedByteOffset(i), newValue);




 148     }

 149 
 150     /**
 151      * Atomically sets the element at position {@code i} to the given
 152      * updated value if the current value {@code ==} the expected value.
 153      *
 154      * @param i the index
 155      * @param expect the expected value
 156      * @param update the new value
 157      * @return true if successful. False return indicates that
 158      * the actual value was not equal to the expected value.
 159      */
 160     public final boolean compareAndSet(int i, long expect, long update) {
 161         return compareAndSetRaw(checkedByteOffset(i), expect, update);
 162     }
 163 
 164     private boolean compareAndSetRaw(long offset, long expect, long update) {
 165         return unsafe.compareAndSwapLong(array, offset, expect, update);
 166     }
 167 
 168     /**
 169      * Atomically sets the element at position {@code i} to the given
 170      * updated value if the current value {@code ==} the expected value.
 171      *
 172      * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
 173      * and does not provide ordering guarantees, so is only rarely an
 174      * appropriate alternative to {@code compareAndSet}.
 175      *
 176      * @param i the index
 177      * @param expect the expected value
 178      * @param update the new value
 179      * @return true if successful
 180      */
 181     public final boolean weakCompareAndSet(int i, long expect, long update) {
 182         return compareAndSet(i, expect, update);
 183     }
 184 
 185     /**
 186      * Atomically increments by one the element at index {@code i}.
 187      *
 188      * @param i the index
 189      * @return the previous value
 190      */
 191     public final long getAndIncrement(int i) {
 192         return getAndAdd(i, 1);
 193     }
 194 
 195     /**
 196      * Atomically decrements by one the element at index {@code i}.
 197      *
 198      * @param i the index
 199      * @return the previous value
 200      */
 201     public final long getAndDecrement(int i) {
 202         return getAndAdd(i, -1);
 203     }
 204 
 205     /**
 206      * Atomically adds the given value to the element at index {@code i}.
 207      *
 208      * @param i the index
 209      * @param delta the value to add
 210      * @return the previous value
 211      */
 212     public final long getAndAdd(int i, long delta) {
 213         return unsafe.getAndAddLong(array, checkedByteOffset(i), delta);




 214     }

 215 
 216     /**
 217      * Atomically increments by one the element at index {@code i}.
 218      *
 219      * @param i the index
 220      * @return the updated value
 221      */
 222     public final long incrementAndGet(int i) {
 223         return getAndAdd(i, 1) + 1;
 224     }
 225 
 226     /**
 227      * Atomically decrements by one the element at index {@code i}.
 228      *
 229      * @param i the index
 230      * @return the updated value
 231      */
 232     public final long decrementAndGet(int i) {
 233         return getAndAdd(i, -1) - 1;
 234     }
 235 
 236     /**
 237      * Atomically adds the given value to the element at index {@code i}.
 238      *
 239      * @param i the index
 240      * @param delta the value to add
 241      * @return the updated value
 242      */
 243     public long addAndGet(int i, long delta) {
 244         return getAndAdd(i, delta) + delta;





 245     }

 246 
 247     /**
 248      * Returns the String representation of the current values of array.
 249      * @return the String representation of the current values of array
 250      */
 251     public String toString() {
 252         int iMax = array.length - 1;
 253         if (iMax == -1)
 254             return "[]";
 255 
 256         StringBuilder b = new StringBuilder();
 257         b.append('[');
 258         for (int i = 0; ; i++) {
 259             b.append(getRaw(byteOffset(i)));
 260             if (i == iMax)
 261                 return b.append(']').toString();
 262             b.append(',').append(' ');
 263         }
 264     }
 265