Class AtomicCounter

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public class AtomicCounter
    extends java.lang.Object
    implements java.lang.AutoCloseable
    Atomic counter that is backed by an AtomicBuffer that can be read across threads and processes.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      AtomicCounter appendToLabel​(java.lang.String suffix)
      Append to the label for a counter constructed with a CountersManager.
      void close()
      Close counter and free the counter slot for reuse of connected to CountersManager.
      boolean compareAndSet​(long expectedValue, long updateValue)
      Compare the current value to expected and if true then set to the update value atomically.
      long decrement()
      Perform an atomic decrement that will not lose updates across threads.
      long decrementOrdered()
      Perform an atomic decrement that is not safe across threads.
      void disconnectCountersManager()
      Disconnect from CountersManager if allocated, so it can be closed without freeing the slot.
      long get()
      Get the latest value for the counter with volatile semantics.
      long getAndAdd​(long increment)
      Add an increment to the counter that will not lose updates across threads.
      long getAndAddOrdered​(long increment)
      Add an increment to the counter with ordered store semantics.
      long getAndSet​(long value)
      Get the current value of a counter and atomically set it to a new value.
      long getWeak()
      Get the value of the counter using weak ordering semantics.
      int id()
      Identity for the counter within the CountersManager.
      long increment()
      Perform an atomic increment that will not lose updates across threads.
      long incrementOrdered()
      Perform an atomic increment that is not safe across threads.
      boolean isClosed()
      Has this counter been closed?
      java.lang.String label()
      Return the label for the counter within the CountersManager.
      boolean proposeMax​(long proposedValue)
      Set the value to a new proposedValue if greater than the current value with memory ordering semantics.
      boolean proposeMaxOrdered​(long proposedValue)
      Set the value to a new proposedValue if greater than the current value with memory ordering semantics.
      void set​(long value)
      Set the counter with volatile semantics.
      void setOrdered​(long value)
      Set the counter with ordered semantics.
      void setWeak​(long value)
      Set the counter with normal semantics.
      java.lang.String toString()
      void updateKey​(java.util.function.Consumer<MutableDirectBuffer> keyFunc)
      Update the key for a counter constructed with a CountersManager.
      void updateKey​(DirectBuffer keyBuffer, int offset, int length)
      Update the key for a counter constructed with a CountersManager.
      void updateLabel​(java.lang.String newLabel)
      Update the label for the counter constructed with a CountersManager.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • isClosed

        private boolean isClosed
      • id

        private final int id
      • addressOffset

        private final long addressOffset
      • byteArray

        private final byte[] byteArray
      • byteBuffer

        private final java.nio.ByteBuffer byteBuffer
    • Constructor Detail

      • AtomicCounter

        public AtomicCounter​(AtomicBuffer buffer,
                             int counterId)
        Map a counter over a buffer. This version will NOT free the counter on close.
        Parameters:
        buffer - containing the counter.
        counterId - identifier of the counter.
      • AtomicCounter

        public AtomicCounter​(AtomicBuffer buffer,
                             int counterId,
                             CountersManager countersManager)
        Map a counter over a buffer. This version will free the counter on close.
        Parameters:
        buffer - containing the counter.
        counterId - identifier for the counter.
        countersManager - to be called to free the counter on close.
    • Method Detail

      • disconnectCountersManager

        public void disconnectCountersManager()
        Disconnect from CountersManager if allocated, so it can be closed without freeing the slot.
      • close

        public void close()
        Close counter and free the counter slot for reuse of connected to CountersManager.
        Specified by:
        close in interface java.lang.AutoCloseable
      • isClosed

        public boolean isClosed()
        Has this counter been closed?
        Returns:
        true if this counter has already been closed.
      • label

        public java.lang.String label()
        Return the label for the counter within the CountersManager.
        Returns:
        the label for the counter within the CountersManager.
      • updateLabel

        public void updateLabel​(java.lang.String newLabel)
        Update the label for the counter constructed with a CountersManager.
        Parameters:
        newLabel - for the counter with a CountersManager.
        Throws:
        java.lang.IllegalStateException - is not constructed CountersManager.
      • appendToLabel

        public AtomicCounter appendToLabel​(java.lang.String suffix)
        Append to the label for a counter constructed with a CountersManager.
        Parameters:
        suffix - for the counter within a CountersManager.
        Returns:
        this for a fluent API.
        Throws:
        java.lang.IllegalStateException - is not constructed CountersManager.
      • updateKey

        public void updateKey​(java.util.function.Consumer<MutableDirectBuffer> keyFunc)
        Update the key for a counter constructed with a CountersManager.
        Parameters:
        keyFunc - callback to use to update the counter's key
        Throws:
        java.lang.IllegalStateException - is not constructed CountersManager.
      • updateKey

        public void updateKey​(DirectBuffer keyBuffer,
                              int offset,
                              int length)
        Update the key for a counter constructed with a CountersManager.
        Parameters:
        keyBuffer - contains key data to be copied into the counter.
        offset - start of the key data within the keyBuffer
        length - length of the data within the keyBuffer (must be <= CountersReader.MAX_KEY_LENGTH)
        Throws:
        java.lang.IllegalStateException - is not constructed CountersManager.
      • increment

        public long increment()
        Perform an atomic increment that will not lose updates across threads.
        Returns:
        the previous value of the counter
      • incrementOrdered

        public long incrementOrdered()
        Perform an atomic increment that is not safe across threads.
        Returns:
        the previous value of the counter
      • decrement

        public long decrement()
        Perform an atomic decrement that will not lose updates across threads.
        Returns:
        the previous value of the counter
      • decrementOrdered

        public long decrementOrdered()
        Perform an atomic decrement that is not safe across threads.
        Returns:
        the previous value of the counter
      • set

        public void set​(long value)
        Set the counter with volatile semantics.
        Parameters:
        value - to be set with volatile semantics.
      • setOrdered

        public void setOrdered​(long value)
        Set the counter with ordered semantics.
        Parameters:
        value - to be set with ordered semantics.
      • setWeak

        public void setWeak​(long value)
        Set the counter with normal semantics.
        Parameters:
        value - to be set with normal semantics.
      • getAndAdd

        public long getAndAdd​(long increment)
        Add an increment to the counter that will not lose updates across threads.
        Parameters:
        increment - to be added.
        Returns:
        the previous value of the counter
      • getAndAddOrdered

        public long getAndAddOrdered​(long increment)
        Add an increment to the counter with ordered store semantics.
        Parameters:
        increment - to be added with ordered store semantics.
        Returns:
        the previous value of the counter
      • getAndSet

        public long getAndSet​(long value)
        Get the current value of a counter and atomically set it to a new value.
        Parameters:
        value - to be set.
        Returns:
        the previous value of the counter
      • compareAndSet

        public boolean compareAndSet​(long expectedValue,
                                     long updateValue)
        Compare the current value to expected and if true then set to the update value atomically.
        Parameters:
        expectedValue - for the counter.
        updateValue - for the counter.
        Returns:
        true if successful otherwise false.
      • get

        public long get()
        Get the latest value for the counter with volatile semantics.
        Returns:
        the latest value for the counter.
      • getWeak

        public long getWeak()
        Get the value of the counter using weak ordering semantics. This is the same a standard read of a field.
        Returns:
        the value for the counter.
      • proposeMax

        public boolean proposeMax​(long proposedValue)
        Set the value to a new proposedValue if greater than the current value with memory ordering semantics.
        Parameters:
        proposedValue - for the new max.
        Returns:
        true if a new max as been set otherwise false.
      • proposeMaxOrdered

        public boolean proposeMaxOrdered​(long proposedValue)
        Set the value to a new proposedValue if greater than the current value with memory ordering semantics.
        Parameters:
        proposedValue - for the new max.
        Returns:
        true if a new max as been set otherwise false.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object