Class 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 Detail

      • ConcurrentAwaitableCounter

        public ConcurrentAwaitableCounter()
    • Method Detail

      • 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.
      • 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