142 143 /** 144 * Eventually sets the element at position {@code i} to the given value. 145 * 146 * @param i the index 147 * @param newValue the new value 148 * @since 1.6 149 */ 150 public final void lazySet(int i, E newValue) { 151 unsafe.putOrderedObject(array, checkedByteOffset(i), newValue); 152 } 153 154 /** 155 * Atomically sets the element at position {@code i} to the given 156 * value and returns the old value. 157 * 158 * @param i the index 159 * @param newValue the new value 160 * @return the previous value 161 */ 162 public final E getAndSet(int i, E newValue) { 163 long offset = checkedByteOffset(i); 164 while (true) { 165 E current = getRaw(offset); 166 if (compareAndSetRaw(offset, current, newValue)) 167 return current; 168 } 169 } 170 171 /** 172 * Atomically sets the element at position {@code i} to the given 173 * updated value if the current value {@code ==} the expected value. 174 * 175 * @param i the index 176 * @param expect the expected value 177 * @param update the new value 178 * @return true if successful. False return indicates that 179 * the actual value was not equal to the expected value. 180 */ 181 public final boolean compareAndSet(int i, E expect, E update) { 182 return compareAndSetRaw(checkedByteOffset(i), expect, update); 183 } 184 185 private boolean compareAndSetRaw(long offset, E expect, E update) { 186 return unsafe.compareAndSwapObject(array, offset, expect, update); 187 } 188 189 /** 190 * Atomically sets the element at position {@code i} to the given 191 * updated value if the current value {@code ==} the expected value. 192 * 193 * <p>May <a href="package-summary.html#Spurious">fail spuriously</a> 194 * and does not provide ordering guarantees, so is only rarely an 195 * appropriate alternative to {@code compareAndSet}. 196 * 197 * @param i the index 198 * @param expect the expected value 199 * @param update the new value 200 * @return true if successful. 201 */ 202 public final boolean weakCompareAndSet(int i, E expect, E update) { 203 return compareAndSet(i, expect, update); 204 } 205 206 /** 207 * Returns the String representation of the current values of array. 208 * @return the String representation of the current values of array 209 */ 210 public String toString() { 211 int iMax = array.length - 1; 212 if (iMax == -1) 213 return "[]"; 214 215 StringBuilder b = new StringBuilder(); 216 b.append('['); 217 for (int i = 0; ; i++) { 218 b.append(getRaw(byteOffset(i))); 219 if (i == iMax) 220 return b.append(']').toString(); | 142 143 /** 144 * Eventually sets the element at position {@code i} to the given value. 145 * 146 * @param i the index 147 * @param newValue the new value 148 * @since 1.6 149 */ 150 public final void lazySet(int i, E newValue) { 151 unsafe.putOrderedObject(array, checkedByteOffset(i), newValue); 152 } 153 154 /** 155 * Atomically sets the element at position {@code i} to the given 156 * value and returns the old value. 157 * 158 * @param i the index 159 * @param newValue the new value 160 * @return the previous value 161 */ 162 @SuppressWarnings("unchecked") 163 public final E getAndSet(int i, E newValue) { 164 return (E)unsafe.getAndSetObject(array, checkedByteOffset(i), newValue); 165 } 166 167 /** 168 * Atomically sets the element at position {@code i} to the given 169 * updated value if the current value {@code ==} the expected value. 170 * 171 * @param i the index 172 * @param expect the expected value 173 * @param update the new value 174 * @return true if successful. False return indicates that 175 * the actual value was not equal to the expected value. 176 */ 177 public final boolean compareAndSet(int i, E expect, E update) { 178 return compareAndSetRaw(checkedByteOffset(i), expect, update); 179 } 180 181 private boolean compareAndSetRaw(long offset, E expect, E update) { 182 return unsafe.compareAndSwapObject(array, offset, expect, update); 183 } 184 185 /** 186 * Atomically sets the element at position {@code i} to the given 187 * updated value if the current value {@code ==} the expected value. 188 * 189 * <p>May <a href="package-summary.html#Spurious">fail spuriously</a> 190 * and does not provide ordering guarantees, so is only rarely an 191 * appropriate alternative to {@code compareAndSet}. 192 * 193 * @param i the index 194 * @param expect the expected value 195 * @param update the new value 196 * @return true if successful 197 */ 198 public final boolean weakCompareAndSet(int i, E expect, E update) { 199 return compareAndSet(i, expect, update); 200 } 201 202 /** 203 * Returns the String representation of the current values of array. 204 * @return the String representation of the current values of array 205 */ 206 public String toString() { 207 int iMax = array.length - 1; 208 if (iMax == -1) 209 return "[]"; 210 211 StringBuilder b = new StringBuilder(); 212 b.append('['); 213 for (int i = 0; ; i++) { 214 b.append(getRaw(byteOffset(i))); 215 if (i == iMax) 216 return b.append(']').toString(); |