Interface AtomicInteger

  • All Known Implementing Classes:
    LocalAtomicInteger, RemoteAtomicInteger

    public interface AtomicInteger
    An int value that may be updated atomically.

    An AtomicInteger is used in applications such as atomically incremented counters, and cannot be used as a replacement for an Integer.

    Since:
    21.12
    Author:
    Aleks Seovic 2020.12.07
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Interface Description
      static class  AtomicInteger.Serializer
      POF serializer implementation.
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      int accumulateAndGet​(int nUpdate, Remote.IntBinaryOperator accumulatorFunction)
      Atomically updates the current value with the results of applying the given function to the current and given values, returning the updated value.
      int accumulateAndGet​(int nUpdate, IntBinaryOperator accumulatorFunction)
      Atomically updates the current value with the results of applying the given function to the current and given values, returning the updated value.
      int addAndGet​(int nDelta)
      Atomically adds the given value to the current value.
      AsyncAtomicInteger async()
      Return non-blocking API for this atomic value.
      byte byteValue()
      Returns the current value of this AtomicInteger as a byte after a narrowing primitive conversion.
      int compareAndExchange​(int nExpectedValue, int nNewValue)
      Atomically sets the value to newValue if the current value, referred to as the witness value, == expectedValue.
      boolean compareAndSet​(int nExpectedValue, int nNewValue)
      Atomically sets the value to newValue if the current value == expectedValue.
      int decrementAndGet()
      Atomically decrements the current value.
      double doubleValue()
      Returns the current value of this AtomicInteger as a double after a widening primitive conversion.
      float floatValue()
      Returns the current value of this AtomicInteger as a float after a widening primitive conversion.
      int get()
      Returns the current value.
      int getAndAccumulate​(int nUpdate, Remote.IntBinaryOperator accumulatorFunction)
      Atomically updates the current value with the results of applying the given function to the current and given values, returning the previous value.
      int getAndAccumulate​(int nUpdate, IntBinaryOperator accumulatorFunction)
      Atomically updates the current value with the results of applying the given function to the current and given values, returning the previous value.
      int getAndAdd​(int nDelta)
      Atomically adds the given value to the current value.
      int getAndDecrement()
      Atomically decrements the current value.
      int getAndIncrement()
      Atomically increments the current value.
      int getAndSet​(int nNewValue)
      Atomically sets the value to newValue and returns the old value.
      int getAndUpdate​(Remote.IntUnaryOperator updateFunction)
      Atomically updates the current value with the results of applying the given function, returning the previous value.
      int getAndUpdate​(IntUnaryOperator updateFunction)
      Atomically updates the current value with the results of applying the given function, returning the previous value.
      int incrementAndGet()
      Atomically increments the current value.
      int intValue()
      Returns the current value of this AtomicInteger as an int.
      long longValue()
      Returns the current value of this AtomicInteger as a long after a widening primitive conversion.
      void set​(int nNewValue)
      Sets the value to newValue.
      short shortValue()
      Returns the current value of this AtomicInteger as a short after a narrowing primitive conversion.
      int updateAndGet​(Remote.IntUnaryOperator updateFunction)
      Atomically updates the current value with the results of applying the given function, returning the updated value.
      int updateAndGet​(IntUnaryOperator updateFunction)
      Atomically updates the current value with the results of applying the given function, returning the updated value.
    • Method Detail

      • async

        AsyncAtomicInteger async()
        Return non-blocking API for this atomic value.
        Returns:
        non-blocking API for this atomic value
      • get

        int get()
        Returns the current value.
        Returns:
        the current value
      • set

        void set​(int nNewValue)
        Sets the value to newValue.
        Parameters:
        nNewValue - the new value
      • getAndSet

        int getAndSet​(int nNewValue)
        Atomically sets the value to newValue and returns the old value.
        Parameters:
        nNewValue - the new value
        Returns:
        the previous value
      • compareAndSet

        boolean compareAndSet​(int nExpectedValue,
                              int nNewValue)
        Atomically sets the value to newValue if the current value == expectedValue.
        Parameters:
        nExpectedValue - the expected value
        nNewValue - the new value
        Returns:
        true if successful. False return indicates that the actual value was not equal to the expected value.
      • getAndIncrement

        int getAndIncrement()
        Atomically increments the current value.

        Equivalent to getAndAdd(1).

        Returns:
        the previous value
      • getAndDecrement

        int getAndDecrement()
        Atomically decrements the current value.

        Equivalent to getAndAdd(-1).

        Returns:
        the previous value
      • getAndAdd

        int getAndAdd​(int nDelta)
        Atomically adds the given value to the current value.
        Parameters:
        nDelta - the value to add
        Returns:
        the previous value
      • incrementAndGet

        int incrementAndGet()
        Atomically increments the current value.

        Equivalent to addAndGet(1).

        Returns:
        the updated value
      • decrementAndGet

        int decrementAndGet()
        Atomically decrements the current value.

        Equivalent to addAndGet(-1).

        Returns:
        the updated value
      • addAndGet

        int addAndGet​(int nDelta)
        Atomically adds the given value to the current value.
        Parameters:
        nDelta - the value to add
        Returns:
        the updated value
      • getAndUpdate

        int getAndUpdate​(Remote.IntUnaryOperator updateFunction)
        Atomically updates 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.
        Parameters:
        updateFunction - a side-effect-free function
        Returns:
        the previous value
      • getAndUpdate

        int getAndUpdate​(IntUnaryOperator updateFunction)
        Atomically updates 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.
        Parameters:
        updateFunction - a side-effect-free function
        Returns:
        the previous value
      • updateAndGet

        int updateAndGet​(Remote.IntUnaryOperator updateFunction)
        Atomically updates 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.
        Parameters:
        updateFunction - a side-effect-free function
        Returns:
        the updated value
      • updateAndGet

        int updateAndGet​(IntUnaryOperator updateFunction)
        Atomically updates 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.
        Parameters:
        updateFunction - a side-effect-free function
        Returns:
        the updated value
      • getAndAccumulate

        int getAndAccumulate​(int nUpdate,
                             Remote.IntBinaryOperator accumulatorFunction)
        Atomically updates the current value with the results of applying the given function to the current and given values, returning the previous value.

        The function should beside-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.

        Parameters:
        nUpdate - the update value
        accumulatorFunction - a side-effect-free function of two arguments
        Returns:
        the previous value
      • getAndAccumulate

        int getAndAccumulate​(int nUpdate,
                             IntBinaryOperator accumulatorFunction)
        Atomically updates the current value with the results of applying the given function to the current and given values, returning the previous value.

        The function should beside-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.

        Parameters:
        nUpdate - the update value
        accumulatorFunction - a side-effect-free function of two arguments
        Returns:
        the previous value
      • accumulateAndGet

        int accumulateAndGet​(int nUpdate,
                             Remote.IntBinaryOperator accumulatorFunction)
        Atomically updates 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.

        Parameters:
        nUpdate - the update value
        accumulatorFunction - a side-effect-free function of two arguments
        Returns:
        the updated value
      • accumulateAndGet

        int accumulateAndGet​(int nUpdate,
                             IntBinaryOperator accumulatorFunction)
        Atomically updates 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.

        Parameters:
        nUpdate - the update value
        accumulatorFunction - a side-effect-free function of two arguments
        Returns:
        the updated value
      • compareAndExchange

        int compareAndExchange​(int nExpectedValue,
                               int nNewValue)
        Atomically sets the value to newValue if the current value, referred to as the witness value, == expectedValue.
        Parameters:
        nExpectedValue - the expected value
        nNewValue - the new value
        Returns:
        the witness value, which will be the same as the expected value if successful
      • intValue

        int intValue()
        Returns the current value of this AtomicInteger as an int.
        Returns:
        the numeric value represented by this object
      • longValue

        long longValue()
        Returns the current value of this AtomicInteger as a long after a widening primitive conversion.
        Returns:
        the numeric value represented by this object after conversion to type long
      • floatValue

        float floatValue()
        Returns the current value of this AtomicInteger as a float after a widening primitive conversion.
        Returns:
        the numeric value represented by this object after conversion to type float
      • doubleValue

        double doubleValue()
        Returns the current value of this AtomicInteger as a double after a widening primitive conversion.
        Returns:
        the numeric value represented by this object after conversion to type double
      • byteValue

        byte byteValue()
        Returns the current value of this AtomicInteger as a byte after a narrowing primitive conversion.
        Returns:
        the numeric value represented by this object after conversion to type byte
      • shortValue

        short shortValue()
        Returns the current value of this AtomicInteger as a short after a narrowing primitive conversion.
        Returns:
        the numeric value represented by this object after conversion to type short