Class FiberUtil


  • public final class FiberUtil
    extends java.lang.Object
    Static utility methods for working with fibers.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <V> java.util.List<V> get​(long time, java.util.concurrent.TimeUnit unit, Fiber<V>... fibers)
      Blocks on the input fibers and creates a new list from the results.
      static <V> java.util.List<V> get​(long timeout, java.util.concurrent.TimeUnit unit, java.util.List<Fiber<V>> fibers)
      Blocks on the input fibers and creates a new list from the results.
      static <V> java.util.List<V> get​(Fiber<V>... fibers)
      Blocks on the input fibers and creates a new list from the results.
      static <V> java.util.List<V> get​(java.util.List<Fiber<V>> fibers)
      Blocks on the input fibers and creates a new list from the results.
      static <V> V runInFiber​(FiberScheduler scheduler, SuspendableCallable<V> target)
      Runs an action in a new fiber, awaits the fiber's termination, and returns its result.
      static void runInFiber​(FiberScheduler scheduler, SuspendableRunnable target)
      Runs an action in a new fiber and awaits the fiber's termination.
      static <V> V runInFiber​(SuspendableCallable<V> target)
      Runs an action in a new fiber, awaits the fiber's termination, and returns its result.
      static void runInFiber​(SuspendableRunnable target)
      Runs an action in a new fiber and awaits the fiber's termination.
      static <V,​X extends java.lang.Exception>
      V
      runInFiberChecked​(FiberScheduler scheduler, SuspendableCallable<V> target, java.lang.Class<X> exceptionType)
      Runs an action in a new fiber, awaits the fiber's termination, and returns its result.
      static <X extends java.lang.Exception>
      void
      runInFiberChecked​(FiberScheduler scheduler, SuspendableRunnable target, java.lang.Class<X> exceptionType)
      Runs an action in a new fiber and awaits the fiber's termination.
      static <V,​X extends java.lang.Exception>
      V
      runInFiberChecked​(SuspendableCallable<V> target, java.lang.Class<X> exceptionType)
      Runs an action in a new fiber, awaits the fiber's termination, and returns its result.
      static <X extends java.lang.Exception>
      void
      runInFiberChecked​(SuspendableRunnable target, java.lang.Class<X> exceptionType)
      Runs an action in a new fiber and awaits the fiber's termination.
      static <V> V runInFiberRuntime​(FiberScheduler scheduler, SuspendableCallable<V> target)
      Runs an action in a new fiber, awaits the fiber's termination, and returns its result.
      static void runInFiberRuntime​(FiberScheduler scheduler, SuspendableRunnable target)
      Runs an action in a new fiber and awaits the fiber's termination.
      static <V> V runInFiberRuntime​(SuspendableCallable<V> target)
      Runs an action in a new fiber, awaits the fiber's termination, and returns its result.
      static void runInFiberRuntime​(SuspendableRunnable target)
      Runs an action in a new fiber and awaits the fiber's termination.
      static <V> java.util.concurrent.Future<V> toFuture​(Fiber<V> fiber)
      Turns a fiber into a Future.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • toFuture

        public static <V> java.util.concurrent.Future<V> toFuture​(Fiber<V> fiber)
        Turns a fiber into a Future.
        Type Parameters:
        V -
        Parameters:
        fiber - the fiber
        Returns:
        a Future representing the fiber.
      • runInFiber

        public static <V> V runInFiber​(SuspendableCallable<V> target)
                                throws java.util.concurrent.ExecutionException,
                                       java.lang.InterruptedException
        Runs an action in a new fiber, awaits the fiber's termination, and returns its result. The new fiber is scheduled by the default scheduler.
        Type Parameters:
        V -
        Parameters:
        target - the operation
        Returns:
        the operations return value
        Throws:
        java.util.concurrent.ExecutionException
        java.lang.InterruptedException
      • runInFiber

        public static <V> V runInFiber​(FiberScheduler scheduler,
                                       SuspendableCallable<V> target)
                                throws java.util.concurrent.ExecutionException,
                                       java.lang.InterruptedException
        Runs an action in a new fiber, awaits the fiber's termination, and returns its result.
        Type Parameters:
        V -
        Parameters:
        scheduler - the FiberScheduler to use when scheduling the fiber.
        target - the operation
        Returns:
        the operations return value
        Throws:
        java.util.concurrent.ExecutionException
        java.lang.InterruptedException
      • runInFiber

        public static void runInFiber​(SuspendableRunnable target)
                               throws java.util.concurrent.ExecutionException,
                                      java.lang.InterruptedException
        Runs an action in a new fiber and awaits the fiber's termination. The new fiber is scheduled by the default scheduler. .
        Parameters:
        target - the operation
        Throws:
        java.util.concurrent.ExecutionException
        java.lang.InterruptedException
      • runInFiber

        public static void runInFiber​(FiberScheduler scheduler,
                                      SuspendableRunnable target)
                               throws java.util.concurrent.ExecutionException,
                                      java.lang.InterruptedException
        Runs an action in a new fiber and awaits the fiber's termination.
        Parameters:
        scheduler - the FiberScheduler to use when scheduling the fiber.
        target - the operation
        Throws:
        java.util.concurrent.ExecutionException
        java.lang.InterruptedException
      • runInFiberRuntime

        public static <V> V runInFiberRuntime​(SuspendableCallable<V> target)
                                       throws java.lang.InterruptedException
        Runs an action in a new fiber, awaits the fiber's termination, and returns its result. Unlike runInFiber this method does not throw ExecutionException, but wraps any checked exception thrown by the operation in a RuntimeException. The new fiber is scheduled by the default scheduler.
        Type Parameters:
        V -
        Parameters:
        target - the operation
        Returns:
        the operations return value
        Throws:
        java.lang.InterruptedException
      • runInFiberRuntime

        public static <V> V runInFiberRuntime​(FiberScheduler scheduler,
                                              SuspendableCallable<V> target)
                                       throws java.lang.InterruptedException
        Runs an action in a new fiber, awaits the fiber's termination, and returns its result. Unlike runInFiber this method does not throw ExecutionException, but wraps any checked exception thrown by the operation in a RuntimeException.
        Type Parameters:
        V -
        Parameters:
        scheduler - the FiberScheduler to use when scheduling the fiber.
        target - the operation
        Returns:
        the operations return value
        Throws:
        java.lang.InterruptedException
      • runInFiberRuntime

        public static void runInFiberRuntime​(SuspendableRunnable target)
                                      throws java.lang.InterruptedException
        Runs an action in a new fiber and awaits the fiber's termination. Unlike runInFiber this method does not throw ExecutionException, but wraps any checked exception thrown by the operation in a RuntimeException. The new fiber is scheduled by the default scheduler.
        Parameters:
        target - the operation
        Throws:
        java.lang.InterruptedException
      • runInFiberRuntime

        public static void runInFiberRuntime​(FiberScheduler scheduler,
                                             SuspendableRunnable target)
                                      throws java.lang.InterruptedException
        Runs an action in a new fiber and awaits the fiber's termination. Unlike runInFiber this method does not throw ExecutionException, but wraps any checked exception thrown by the operation in a RuntimeException.
        Parameters:
        scheduler - the FiberScheduler to use when scheduling the fiber.
        target - the operation
        Throws:
        java.lang.InterruptedException
      • runInFiberChecked

        public static <V,​X extends java.lang.Exception> V runInFiberChecked​(SuspendableCallable<V> target,
                                                                                  java.lang.Class<X> exceptionType)
                                                                           throws X extends java.lang.Exception,
                                                                                  java.lang.InterruptedException
        Runs an action in a new fiber, awaits the fiber's termination, and returns its result. Unlike runInFiber this method does not throw ExecutionException, but wraps any checked exception thrown by the operation in a RuntimeException. The new fiber is scheduled by the default scheduler.
        Type Parameters:
        V -
        Parameters:
        target - the operation
        Returns:
        the operations return value
        Throws:
        java.lang.InterruptedException
        X extends java.lang.Exception
      • runInFiberChecked

        public static <V,​X extends java.lang.Exception> V runInFiberChecked​(FiberScheduler scheduler,
                                                                                  SuspendableCallable<V> target,
                                                                                  java.lang.Class<X> exceptionType)
                                                                           throws X extends java.lang.Exception,
                                                                                  java.lang.InterruptedException
        Runs an action in a new fiber, awaits the fiber's termination, and returns its result. Unlike runInFiber this method does not throw ExecutionException, but wraps any checked exception thrown by the operation in a RuntimeException, unless it is of the given exception type, in which case the checked exception is thrown as-is.
        Type Parameters:
        V -
        Parameters:
        scheduler - the FiberScheduler to use when scheduling the fiber.
        target - the operation
        exceptionType - a checked exception type that will not be wrapped if thrown by the operation, but thrown as-is.
        Returns:
        the operations return value
        Throws:
        java.lang.InterruptedException
        X extends java.lang.Exception
      • runInFiberChecked

        public static <X extends java.lang.Exception> void runInFiberChecked​(SuspendableRunnable target,
                                                                             java.lang.Class<X> exceptionType)
                                                                      throws X extends java.lang.Exception,
                                                                             java.lang.InterruptedException
        Runs an action in a new fiber and awaits the fiber's termination. Unlike runInFiber this method does not throw ExecutionException, but wraps any checked exception thrown by the operation in a RuntimeException, unless it is of the given exception type, in which case the checked exception is thrown as-is. The new fiber is scheduled by the default scheduler.
        Parameters:
        target - the operation
        exceptionType - a checked exception type that will not be wrapped if thrown by the operation, but thrown as-is.
        Throws:
        java.lang.InterruptedException
        X extends java.lang.Exception
      • runInFiberChecked

        public static <X extends java.lang.Exception> void runInFiberChecked​(FiberScheduler scheduler,
                                                                             SuspendableRunnable target,
                                                                             java.lang.Class<X> exceptionType)
                                                                      throws X extends java.lang.Exception,
                                                                             java.lang.InterruptedException
        Runs an action in a new fiber and awaits the fiber's termination. Unlike runInFiber this method does not throw ExecutionException, but wraps any checked exception thrown by the operation in a RuntimeException, unless it is of the given exception type, in which case the checked exception is thrown as-is.
        Parameters:
        scheduler - the FiberScheduler to use when scheduling the fiber.
        target - the operation
        exceptionType - a checked exception type that will not be wrapped if thrown by the operation, but thrown as-is.
        Throws:
        java.lang.InterruptedException
        X extends java.lang.Exception
      • get

        public static <V> java.util.List<V> get​(java.util.List<Fiber<V>> fibers)
                                         throws java.lang.InterruptedException
        Blocks on the input fibers and creates a new list from the results. The result list is the same order as the input list.
        Parameters:
        fibers - to combine
        Throws:
        java.lang.InterruptedException
      • get

        public static <V> java.util.List<V> get​(Fiber<V>... fibers)
                                         throws java.lang.InterruptedException
        Blocks on the input fibers and creates a new list from the results. The result list is the same order as the input list.
        Parameters:
        fibers - to combine
        Throws:
        java.lang.InterruptedException
      • get

        public static <V> java.util.List<V> get​(long timeout,
                                                java.util.concurrent.TimeUnit unit,
                                                java.util.List<Fiber<V>> fibers)
                                         throws java.lang.InterruptedException,
                                                java.util.concurrent.TimeoutException
        Blocks on the input fibers and creates a new list from the results. The result list is the same order as the input list.
        Parameters:
        timeout - to wait for all requests to complete
        unit - the time is in
        fibers - to combine
        Throws:
        java.lang.InterruptedException
        java.util.concurrent.TimeoutException
      • get

        public static <V> java.util.List<V> get​(long time,
                                                java.util.concurrent.TimeUnit unit,
                                                Fiber<V>... fibers)
                                         throws java.lang.InterruptedException,
                                                java.util.concurrent.TimeoutException
        Blocks on the input fibers and creates a new list from the results. The result list is the same order as the input list.
        Parameters:
        time - to wait for all requests to complete
        unit - the time is in
        fibers - to combine
        Throws:
        java.lang.InterruptedException
        java.util.concurrent.TimeoutException