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

Print this page




  98         value = newValue;
  99     }
 100 
 101     /**
 102      * Eventually sets to the given value.
 103      *
 104      * @param newValue the new value
 105      * @since 1.6
 106      */
 107     public final void lazySet(int newValue) {
 108         unsafe.putOrderedInt(this, valueOffset, newValue);
 109     }
 110 
 111     /**
 112      * Atomically sets to the given value and returns the old value.
 113      *
 114      * @param newValue the new value
 115      * @return the previous value
 116      */
 117     public final int getAndSet(int newValue) {
 118         for (;;) {
 119             int current = get();
 120             if (compareAndSet(current, newValue))
 121                 return current;
 122         }
 123     }
 124 
 125     /**
 126      * Atomically sets the value to the given updated value
 127      * if the current value {@code ==} the expected value.
 128      *
 129      * @param expect the expected value
 130      * @param update the new value
 131      * @return true if successful. False return indicates that
 132      * the actual value was not equal to the expected value.
 133      */
 134     public final boolean compareAndSet(int expect, int update) {
 135         return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
 136     }
 137 
 138     /**
 139      * Atomically sets the value to the given updated value
 140      * if the current value {@code ==} the expected value.
 141      *
 142      * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
 143      * and does not provide ordering guarantees, so is only rarely an
 144      * appropriate alternative to {@code compareAndSet}.
 145      *
 146      * @param expect the expected value
 147      * @param update the new value
 148      * @return true if successful.
 149      */
 150     public final boolean weakCompareAndSet(int expect, int update) {
 151         return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
 152     }
 153 
 154     /**
 155      * Atomically increments by one the current value.
 156      *
 157      * @return the previous value
 158      */
 159     public final int getAndIncrement() {
 160         for (;;) {
 161             int current = get();
 162             int next = current + 1;
 163             if (compareAndSet(current, next))
 164                 return current;
 165         }
 166     }
 167 
 168     /**
 169      * Atomically decrements by one the current value.
 170      *
 171      * @return the previous value
 172      */
 173     public final int getAndDecrement() {
 174         for (;;) {
 175             int current = get();
 176             int next = current - 1;
 177             if (compareAndSet(current, next))
 178                 return current;
 179         }
 180     }
 181 
 182     /**
 183      * Atomically adds the given value to the current value.
 184      *
 185      * @param delta the value to add
 186      * @return the previous value
 187      */
 188     public final int getAndAdd(int delta) {
 189         for (;;) {
 190             int current = get();
 191             int next = current + delta;
 192             if (compareAndSet(current, next))
 193                 return current;
 194         }
 195     }
 196 
 197     /**
 198      * Atomically increments by one the current value.
 199      *
 200      * @return the updated value
 201      */
 202     public final int incrementAndGet() {
 203         for (;;) {
 204             int current = get();
 205             int next = current + 1;
 206             if (compareAndSet(current, next))
 207                 return next;
 208         }
 209     }
 210 
 211     /**
 212      * Atomically decrements by one the current value.
 213      *
 214      * @return the updated value
 215      */
 216     public final int decrementAndGet() {
 217         for (;;) {
 218             int current = get();
 219             int next = current - 1;
 220             if (compareAndSet(current, next))
 221                 return next;
 222         }
 223     }
 224 
 225     /**
 226      * Atomically adds the given value to the current value.
 227      *
 228      * @param delta the value to add
 229      * @return the updated value
 230      */
 231     public final int addAndGet(int delta) {
 232         for (;;) {
 233             int current = get();
 234             int next = current + delta;
 235             if (compareAndSet(current, next))
 236                 return next;
 237         }
 238     }
 239 
 240     /**
 241      * Returns the String representation of the current value.
 242      * @return the String representation of the current value.
 243      */
 244     public String toString() {
 245         return Integer.toString(get());
 246     }
 247 
 248     /**
 249      * Returns the value of this {@code AtomicInteger} as an {@code int}.
 250      */
 251     public int intValue() {
 252         return get();
 253     }
 254 
 255     /**
 256      * Returns the value of this {@code AtomicInteger} as a {@code long}
 257      * after a widening primitive conversion.
 258      * @jls 5.1.2 Widening Primitive Conversions
 259      */
 260     public long longValue() {
 261         return (long)get();
 262     }


  98         value = newValue;
  99     }
 100 
 101     /**
 102      * Eventually sets to the given value.
 103      *
 104      * @param newValue the new value
 105      * @since 1.6
 106      */
 107     public final void lazySet(int newValue) {
 108         unsafe.putOrderedInt(this, valueOffset, newValue);
 109     }
 110 
 111     /**
 112      * Atomically sets to the given value and returns the old value.
 113      *
 114      * @param newValue the new value
 115      * @return the previous value
 116      */
 117     public final int getAndSet(int newValue) {
 118         return unsafe.getAndSetInt(this, valueOffset, newValue);



 119     }

 120 
 121     /**
 122      * Atomically sets the value to the given updated value
 123      * if the current value {@code ==} the expected value.
 124      *
 125      * @param expect the expected value
 126      * @param update the new value
 127      * @return true if successful. False return indicates that
 128      * the actual value was not equal to the expected value.
 129      */
 130     public final boolean compareAndSet(int expect, int update) {
 131         return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
 132     }
 133 
 134     /**
 135      * Atomically sets the value to the given updated value
 136      * if the current value {@code ==} the expected value.
 137      *
 138      * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
 139      * and does not provide ordering guarantees, so is only rarely an
 140      * appropriate alternative to {@code compareAndSet}.
 141      *
 142      * @param expect the expected value
 143      * @param update the new value
 144      * @return true if successful
 145      */
 146     public final boolean weakCompareAndSet(int expect, int update) {
 147         return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
 148     }
 149 
 150     /**
 151      * Atomically increments by one the current value.
 152      *
 153      * @return the previous value
 154      */
 155     public final int getAndIncrement() {
 156         return getAndAdd(1);




 157     }

 158 
 159     /**
 160      * Atomically decrements by one the current value.
 161      *
 162      * @return the previous value
 163      */
 164     public final int getAndDecrement() {
 165         return getAndAdd(-1);




 166     }

 167 
 168     /**
 169      * Atomically adds the given value to the current value.
 170      *
 171      * @param delta the value to add
 172      * @return the previous value
 173      */
 174     public final int getAndAdd(int delta) {
 175         return unsafe.getAndAddInt(this, valueOffset, delta);




 176     }

 177 
 178     /**
 179      * Atomically increments by one the current value.
 180      *
 181      * @return the updated value
 182      */
 183     public final int incrementAndGet() {
 184         return getAndAdd(1) + 1;




 185     }

 186 
 187     /**
 188      * Atomically decrements by one the current value.
 189      *
 190      * @return the updated value
 191      */
 192     public final int decrementAndGet() {
 193         return getAndAdd(-1) - 1;




 194     }

 195 
 196     /**
 197      * Atomically adds the given value to the current value.
 198      *
 199      * @param delta the value to add
 200      * @return the updated value
 201      */
 202     public final int addAndGet(int delta) {
 203         return getAndAdd(delta) + delta;




 204     }

 205 
 206     /**
 207      * Returns the String representation of the current value.
 208      * @return the String representation of the current value
 209      */
 210     public String toString() {
 211         return Integer.toString(get());
 212     }
 213 
 214     /**
 215      * Returns the value of this {@code AtomicInteger} as an {@code int}.
 216      */
 217     public int intValue() {
 218         return get();
 219     }
 220 
 221     /**
 222      * Returns the value of this {@code AtomicInteger} as a {@code long}
 223      * after a widening primitive conversion.
 224      * @jls 5.1.2 Widening Primitive Conversions
 225      */
 226     public long longValue() {
 227         return (long)get();
 228     }