Class Sequence

Direct Known Subclasses:
FixedSequenceGroup, NoOpEventProcessor.SequencerFollowingSequence, SequenceGroup

public class Sequence extends RhsPadding

Concurrent sequence class used for tracking the progress of the ring buffer and event processors. Support a number of concurrent operations including CAS and order writes.

Also attempts to be more efficient with regards to false sharing by adding padding around the volatile field.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    (package private) static final long
     
    private static final sun.misc.Unsafe
     
    private static final long
     

    Fields inherited from class com.lmax.disruptor.RhsPadding

    p10, p11, p12, p13, p14, p15, p9

    Fields inherited from class com.lmax.disruptor.Value

    value

    Fields inherited from class com.lmax.disruptor.LhsPadding

    p1, p2, p3, p4, p5, p6, p7
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a sequence initialised to -1.
    Sequence(long initialValue)
    Create a sequence with a specified initial value.
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    addAndGet(long increment)
    Atomically add the supplied value.
    boolean
    compareAndSet(long expectedValue, long newValue)
    Perform a compare and set operation on the sequence.
    long
    get()
    Perform a volatile read of this sequence's value.
    long
    Atomically increment the sequence by one.
    void
    set(long value)
    Perform an ordered write of this sequence.
    void
    setVolatile(long value)
    Performs a volatile write of this sequence.
     

    Methods inherited from class java.lang.Object

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

    • INITIAL_VALUE

      static final long INITIAL_VALUE
      See Also:
    • UNSAFE

      private static final sun.misc.Unsafe UNSAFE
    • VALUE_OFFSET

      private static final long VALUE_OFFSET
  • Constructor Details

    • Sequence

      public Sequence()
      Create a sequence initialised to -1.
    • Sequence

      public Sequence(long initialValue)
      Create a sequence with a specified initial value.
      Parameters:
      initialValue - The initial value for this sequence.
  • Method Details

    • get

      public long get()
      Perform a volatile read of this sequence's value.
      Returns:
      The current value of the sequence.
    • set

      public void set(long value)
      Perform an ordered write of this sequence. The intent is a Store/Store barrier between this write and any previous store.
      Parameters:
      value - The new value for the sequence.
    • setVolatile

      public void setVolatile(long value)
      Performs a volatile write of this sequence. The intent is a Store/Store barrier between this write and any previous write and a Store/Load barrier between this write and any subsequent volatile read.
      Parameters:
      value - The new value for the sequence.
    • compareAndSet

      public boolean compareAndSet(long expectedValue, long newValue)
      Perform a compare and set operation on the sequence.
      Parameters:
      expectedValue - The expected current value.
      newValue - The value to update to.
      Returns:
      true if the operation succeeds, false otherwise.
    • incrementAndGet

      public long incrementAndGet()
      Atomically increment the sequence by one.
      Returns:
      The value after the increment
    • addAndGet

      public long addAndGet(long increment)
      Atomically add the supplied value.
      Parameters:
      increment - The value to add to the sequence.
      Returns:
      The value after the increment.
    • toString

      public String toString()
      Overrides:
      toString in class Object