Class ConcurrentAwaitableCounter

java.lang.Object
org.apache.druid.concurrent.ConcurrentAwaitableCounter

public final class ConcurrentAwaitableCounter extends Object
This synchronization object allows to increment() a counter without blocking, potentially from multiple threads (although in some use cases there is just one incrementer thread), and block in other thread(s), awaiting when the count reaches the provided value: see awaitCount(long), or the specified number of events since the call: see awaitNextIncrements(long). This counter wraps around Long.MAX_VALUE and starts from 0 again, so "next" count should be generally obtained by calling nextCount(currentCount) rather than currentCount + 1. Memory consistency effects: actions in threads prior to calling increment() while the count was less than the awaited value happen-before actions following count awaiting methods such as awaitCount(long).
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    awaitCount(long totalCount)
    Await until the increment() is called on this counter object the specified number of times from the creation of this counter object.
    void
    awaitCount(long totalCount, long timeout, TimeUnit unit)
    Await until the increment() is called on this counter object the specified number of times from the creation of this counter object, for not longer than the specified period of time.
    boolean
    awaitFirstIncrement(long timeout, TimeUnit unit)
    The difference between this method and awaitCount(long, long, TimeUnit) with argument 1 is that awaitFirstIncrement() returns boolean designating whether the count was await (while waiting for no longer than for the specified period of time), while awaitCount() throws TimeoutException if the count was not awaited.
    void
    awaitNextIncrements(long nextIncrements)
    Somewhat loosely defined wait for "next N increments", because the starting point is not defined from the Java Memory Model perspective.
    void
    Increment the count.
    static long
    nextCount(long prevCount)
    This method should be called to obtain the next total increment count to be passed to awaitCount(long) methods, instead of just adding 1 to the previous count, because the count must wrap around Long.MAX_VALUE and start from 0 again.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ConcurrentAwaitableCounter

      public ConcurrentAwaitableCounter()
  • Method Details

    • nextCount

      public static long nextCount(long prevCount)
      This method should be called to obtain the next total increment count to be passed to awaitCount(long) methods, instead of just adding 1 to the previous count, because the count must wrap around Long.MAX_VALUE and start from 0 again.
    • increment

      public void increment()
      Increment the count. This method could be safely called from concurrent threads.
    • awaitCount

      public void awaitCount(long totalCount) throws InterruptedException
      Await until the increment() is called on this counter object the specified number of times from the creation of this counter object.
      Throws:
      InterruptedException
    • awaitCount

      public void awaitCount(long totalCount, long timeout, TimeUnit unit) throws InterruptedException, TimeoutException
      Await until the increment() is called on this counter object the specified number of times from the creation of this counter object, for not longer than the specified period of time. If by this time the target increment count is not reached, TimeoutException is thrown.
      Throws:
      InterruptedException
      TimeoutException
    • awaitNextIncrements

      public void awaitNextIncrements(long nextIncrements) throws InterruptedException
      Somewhat loosely defined wait for "next N increments", because the starting point is not defined from the Java Memory Model perspective.
      Throws:
      InterruptedException
    • awaitFirstIncrement

      public boolean awaitFirstIncrement(long timeout, TimeUnit unit) throws InterruptedException
      The difference between this method and awaitCount(long, long, TimeUnit) with argument 1 is that awaitFirstIncrement() returns boolean designating whether the count was await (while waiting for no longer than for the specified period of time), while awaitCount() throws TimeoutException if the count was not awaited.
      Throws:
      InterruptedException