Class ReusableFutureLatch<T>

  • Type Parameters:
    T - The type of the futures that this class works with.

    public class ReusableFutureLatch<T>
    extends java.lang.Object
    This class is similar to ReusableLatch but that works with CompletableFuture so that blocking can be async and exceptions and results can be passed.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void register​(java.util.concurrent.CompletableFuture<T> toNotify)
      Supply a future to be notified when release(Object) is called.
      void registerAndRunReleaser​(java.lang.Runnable willCallRelease, java.util.concurrent.CompletableFuture<T> toNotify)
      If the latch is released, completes the provided future without invoking the provided runnable.
      void release​(T result)
      Complete all waiting futures, and all future calls to register be notified immediately.
      void releaseExceptionally​(java.lang.Throwable e)
      Complete all waiting futures, and all future calls to register be notified immediately.
      void releaseExceptionallyAndReset​(java.lang.Throwable e)
      Identical to calling #releaseExceptionally(Exception); #reset() except it is atomic.
      void reset()
      If release(Object) or releaseExceptionally(Throwable) has been called it resets the object into the unreleased state.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

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

      • ReusableFutureLatch

        public ReusableFutureLatch()
    • Method Detail

      • register

        public void register​(java.util.concurrent.CompletableFuture<T> toNotify)
        Supply a future to be notified when release(Object) is called. If release has already been called, it will be completed immediately.
        Parameters:
        toNotify - The future that should be completed.
      • registerAndRunReleaser

        public void registerAndRunReleaser​(java.lang.Runnable willCallRelease,
                                           java.util.concurrent.CompletableFuture<T> toNotify)
        If the latch is released, completes the provided future without invoking the provided runnable. If the latch is not released it will add the provided future to the list to be notified and runs the provided runnable if there is not already one running. If there are multiple calls to this method, only the runnable of one will be invoked. If the runnable throws the exception will be thrown to the caller of this method, and future callers may have their runnable method invoked (Presuming that the latch is not released)
        Parameters:
        willCallRelease - A runnable that should result in release(Object) being called.
        toNotify - The future to notify once release is called.
      • release

        public void release​(T result)
        Complete all waiting futures, and all future calls to register be notified immediately. If release is called twice consecutively the second value will be the one passed to future callers of register(CompletableFuture)
        Parameters:
        result - The result to pass to waiting futures.
      • releaseExceptionally

        public void releaseExceptionally​(java.lang.Throwable e)
        Complete all waiting futures, and all future calls to register be notified immediately. If release is called twice consecutively the second value will be the one passed to future callers of register(CompletableFuture)
        Parameters:
        e - The exception to pass to waiting futures.
      • releaseExceptionallyAndReset

        public void releaseExceptionallyAndReset​(java.lang.Throwable e)
        Identical to calling #releaseExceptionally(Exception); #reset() except it is atomic.
        Parameters:
        e - The exception to fail all waiting futures with.
      • toString

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