Java atomicinteger как работает

Class AtomicInteger

An int value that may be updated atomically. See the VarHandle specification for descriptions of the properties of atomic accesses. An AtomicInteger is used in applications such as atomically incremented counters, and cannot be used as a replacement for an Integer . However, this class does extend Number to allow uniform access by tools and utilities that deal with numerically-based classes.

Constructor Summary

Method Summary

Atomically updates (with memory effects as specified by VarHandle.compareAndSet(java.lang.Object. ) ) the current value with the results of applying the given function to the current and given values, returning the updated value.

Atomically adds the given value to the current value, with memory effects as specified by VarHandle.getAndAdd(java.lang.Object. ) .

Atomically sets the value to newValue if the current value, referred to as the witness value, == expectedValue , with memory effects as specified by VarHandle.compareAndExchange(java.lang.Object. ) .

Atomically sets the value to newValue if the current value, referred to as the witness value, == expectedValue , with memory effects as specified by VarHandle.compareAndExchangeAcquire(java.lang.Object. ) .

Atomically sets the value to newValue if the current value, referred to as the witness value, == expectedValue , with memory effects as specified by VarHandle.compareAndExchangeRelease(java.lang.Object. ) .

Atomically sets the value to newValue if the current value == expectedValue , with memory effects as specified by VarHandle.compareAndSet(java.lang.Object. ) .

Читайте также:  Цикл внутри массива python

Atomically decrements the current value, with memory effects as specified by VarHandle.getAndAdd(java.lang.Object. ) .

Returns the current value of this AtomicInteger as a double after a widening primitive conversion, with memory effects as specified by VarHandle.getVolatile(java.lang.Object. ) .

Returns the current value of this AtomicInteger as a float after a widening primitive conversion, with memory effects as specified by VarHandle.getVolatile(java.lang.Object. ) .

Returns the current value, with memory effects as specified by VarHandle.getVolatile(java.lang.Object. ) .

Returns the current value, with memory effects as specified by VarHandle.getAcquire(java.lang.Object. ) .

Atomically updates (with memory effects as specified by VarHandle.compareAndSet(java.lang.Object. ) ) the current value with the results of applying the given function to the current and given values, returning the previous value.

Atomically adds the given value to the current value, with memory effects as specified by VarHandle.getAndAdd(java.lang.Object. ) .

Atomically decrements the current value, with memory effects as specified by VarHandle.getAndAdd(java.lang.Object. ) .

Atomically increments the current value, with memory effects as specified by VarHandle.getAndAdd(java.lang.Object. ) .

Atomically sets the value to newValue and returns the old value, with memory effects as specified by VarHandle.getAndSet(java.lang.Object. ) .

Atomically updates (with memory effects as specified by VarHandle.compareAndSet(java.lang.Object. ) ) the current value with the results of applying the given function, returning the previous value.

Returns the current value, with memory effects as specified by VarHandle.getOpaque(java.lang.Object. ) .

Returns the current value, with memory semantics of reading as if the variable was declared non- volatile .

Atomically increments the current value, with memory effects as specified by VarHandle.getAndAdd(java.lang.Object. ) .

Returns the current value of this AtomicInteger as an int , with memory effects as specified by VarHandle.getVolatile(java.lang.Object. ) .

Sets the value to newValue , with memory effects as specified by VarHandle.setRelease(java.lang.Object. ) .

Returns the current value of this AtomicInteger as a long after a widening primitive conversion, with memory effects as specified by VarHandle.getVolatile(java.lang.Object. ) .

Sets the value to newValue , with memory effects as specified by VarHandle.setVolatile(java.lang.Object. ) .

Sets the value to newValue , with memory effects as specified by VarHandle.setOpaque(java.lang.Object. ) .

Sets the value to newValue , with memory semantics of setting as if the variable was declared non- volatile and non- final .

Sets the value to newValue , with memory effects as specified by VarHandle.setRelease(java.lang.Object. ) .

Atomically updates (with memory effects as specified by VarHandle.compareAndSet(java.lang.Object. ) ) the current value with the results of applying the given function, returning the updated value.

This method has plain memory effects but the method name implies volatile memory effects (see methods such as compareAndExchange(int, int) and compareAndSet(int, int) ).

Possibly atomically sets the value to newValue if the current value == expectedValue , with memory effects as specified by VarHandle.weakCompareAndSetAcquire(java.lang.Object. ) .

Possibly atomically sets the value to newValue if the current value == expectedValue , with memory effects as specified by VarHandle.weakCompareAndSetPlain(java.lang.Object. ) .

Possibly atomically sets the value to newValue if the current value == expectedValue , with memory effects as specified by VarHandle.weakCompareAndSetRelease(java.lang.Object. ) .

Possibly atomically sets the value to newValue if the current value == expectedValue , with memory effects as specified by VarHandle.weakCompareAndSet(java.lang.Object. ) .

Methods declared in class java.lang.Number

Methods declared in class java.lang.Object

Constructor Details

AtomicInteger

AtomicInteger

Method Details

get

Returns the current value, with memory effects as specified by VarHandle.getVolatile(java.lang.Object. ) .

set

Sets the value to newValue , with memory effects as specified by VarHandle.setVolatile(java.lang.Object. ) .

lazySet

Sets the value to newValue , with memory effects as specified by VarHandle.setRelease(java.lang.Object. ) .

getAndSet

Atomically sets the value to newValue and returns the old value, with memory effects as specified by VarHandle.getAndSet(java.lang.Object. ) .

compareAndSet

Atomically sets the value to newValue if the current value == expectedValue , with memory effects as specified by VarHandle.compareAndSet(java.lang.Object. ) .

weakCompareAndSet

This method has plain memory effects but the method name implies volatile memory effects (see methods such as compareAndExchange(int, int) and compareAndSet(int, int) ). To avoid confusion over plain or volatile memory effects it is recommended that the method weakCompareAndSetPlain(int, int) be used instead.

Possibly atomically sets the value to newValue if the current value == expectedValue , with memory effects as specified by VarHandle.weakCompareAndSetPlain(java.lang.Object. ) .

weakCompareAndSetPlain

Possibly atomically sets the value to newValue if the current value == expectedValue , with memory effects as specified by VarHandle.weakCompareAndSetPlain(java.lang.Object. ) .

getAndIncrement

Atomically increments the current value, with memory effects as specified by VarHandle.getAndAdd(java.lang.Object. ) . Equivalent to getAndAdd(1) .

getAndDecrement

Atomically decrements the current value, with memory effects as specified by VarHandle.getAndAdd(java.lang.Object. ) . Equivalent to getAndAdd(-1) .

getAndAdd

Atomically adds the given value to the current value, with memory effects as specified by VarHandle.getAndAdd(java.lang.Object. ) .

incrementAndGet

Atomically increments the current value, with memory effects as specified by VarHandle.getAndAdd(java.lang.Object. ) . Equivalent to addAndGet(1) .

decrementAndGet

Atomically decrements the current value, with memory effects as specified by VarHandle.getAndAdd(java.lang.Object. ) . Equivalent to addAndGet(-1) .

addAndGet

Atomically adds the given value to the current value, with memory effects as specified by VarHandle.getAndAdd(java.lang.Object. ) .

getAndUpdate

Atomically updates (with memory effects as specified by VarHandle.compareAndSet(java.lang.Object. ) ) the current value with the results of applying the given function, returning the previous value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads.

updateAndGet

Atomically updates (with memory effects as specified by VarHandle.compareAndSet(java.lang.Object. ) ) the current value with the results of applying the given function, returning the updated value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads.

getAndAccumulate

Atomically updates (with memory effects as specified by VarHandle.compareAndSet(java.lang.Object. ) ) the current value with the results of applying the given function to the current and given values, returning the previous value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads. The function is applied with the current value as its first argument, and the given update as the second argument.

accumulateAndGet

Atomically updates (with memory effects as specified by VarHandle.compareAndSet(java.lang.Object. ) ) the current value with the results of applying the given function to the current and given values, returning the updated value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads. The function is applied with the current value as its first argument, and the given update as the second argument.

toString

intValue

Returns the current value of this AtomicInteger as an int , with memory effects as specified by VarHandle.getVolatile(java.lang.Object. ) . Equivalent to get() .

longValue

Returns the current value of this AtomicInteger as a long after a widening primitive conversion, with memory effects as specified by VarHandle.getVolatile(java.lang.Object. ) .

floatValue

Returns the current value of this AtomicInteger as a float after a widening primitive conversion, with memory effects as specified by VarHandle.getVolatile(java.lang.Object. ) .

doubleValue

Returns the current value of this AtomicInteger as a double after a widening primitive conversion, with memory effects as specified by VarHandle.getVolatile(java.lang.Object. ) .

getPlain

Returns the current value, with memory semantics of reading as if the variable was declared non- volatile .

setPlain

Sets the value to newValue , with memory semantics of setting as if the variable was declared non- volatile and non- final .

getOpaque

Returns the current value, with memory effects as specified by VarHandle.getOpaque(java.lang.Object. ) .

setOpaque

Sets the value to newValue , with memory effects as specified by VarHandle.setOpaque(java.lang.Object. ) .

getAcquire

Returns the current value, with memory effects as specified by VarHandle.getAcquire(java.lang.Object. ) .

setRelease

Sets the value to newValue , with memory effects as specified by VarHandle.setRelease(java.lang.Object. ) .

compareAndExchange

Atomically sets the value to newValue if the current value, referred to as the witness value, == expectedValue , with memory effects as specified by VarHandle.compareAndExchange(java.lang.Object. ) .

compareAndExchangeAcquire

Atomically sets the value to newValue if the current value, referred to as the witness value, == expectedValue , with memory effects as specified by VarHandle.compareAndExchangeAcquire(java.lang.Object. ) .

compareAndExchangeRelease

Atomically sets the value to newValue if the current value, referred to as the witness value, == expectedValue , with memory effects as specified by VarHandle.compareAndExchangeRelease(java.lang.Object. ) .

weakCompareAndSetVolatile

Possibly atomically sets the value to newValue if the current value == expectedValue , with memory effects as specified by VarHandle.weakCompareAndSet(java.lang.Object. ) .

weakCompareAndSetAcquire

Possibly atomically sets the value to newValue if the current value == expectedValue , with memory effects as specified by VarHandle.weakCompareAndSetAcquire(java.lang.Object. ) .

weakCompareAndSetRelease

Possibly atomically sets the value to newValue if the current value == expectedValue , with memory effects as specified by VarHandle.weakCompareAndSetRelease(java.lang.Object. ) .

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Оцените статью