Package fj

Class FJ


public class FJ extends Object
Author:
Dennis Cosgrove
  • Constructor Details

    • FJ

      public FJ()
  • Method Details

    • fork

      public static <T> Future<T> fork(TaskSupplier<T> task)
      Indicates that work defined by a value returning task can be left behind for another idle thread to possibly steal and execute. This method does not block. One can think of this method as creating a fork in the road for the work of the specified task, and then continuing forward along the current path. Another thread is free to pick up this "left behind" work and proceed down its "forked" path. This method returns a Future which can be later joined to: 1) wait for the task to complete, if it has not already, and 2) acquire the supplied result. Note: only invoke this method when the work of the specified task can run before, during, or after all statements which follow it, up to the joining of its returned Future.
      Type Parameters:
      T - return type of the task
      Parameters:
      task - a value supplying task of work
      Returns:
      a Future which can be joined to force the task's completion and acquire its supplied value
    • void_fork

      public static Future<Void> void_fork(TaskRunnable task)
      Indicates that work defined by a void returning task can be left behind for another idle thread to possibly steal and execute. This method does not block. Leaves the work of the specified task behind, as fork(fj.api.TaskSupplier<T>) does, but for a task which does not return a value.
      Parameters:
      task - a task of work which does not return a value
      Returns:
      a Future which can be joined to force the task's completion
      See Also:
    • join

      public static <R> R join(Future<R> future) throws InterruptedException, ExecutionException
      Blocks, if necessary, for the task associated with the future to complete and returns the task's result.
      Type Parameters:
      R - the return type of the future
      Parameters:
      future - the future of an associated forked task
      Returns:
      the value returned by the task
      Throws:
      InterruptedException - if the current thread was interrupted while waiting
      ExecutionException - if the task associated with the future threw an exception
    • join

      public static <R> List<R> join(Future<R>[] futures) throws InterruptedException, ExecutionException
      Blocks, if necessary, for the tasks associated with the futures to complete and returns the tasks' results as a List, preserving their order.
      Type Parameters:
      R - return type of each task and, it follows, the type of each element in the returned List
      Parameters:
      futures - futures associated with forked tasks
      Returns:
      Throws:
      InterruptedException - if the current thread was interrupted while waiting
      ExecutionException - if the task associated with the future threw an exception
    • join

      public static <R> R[] join(IntFunction<R[]> returnValueArrayCreator, Future<R>[] futures) throws InterruptedException, ExecutionException
      Blocks, if necessary, for the tasks associated with the futures to complete and returns the tasks' results as an array, preserving their order.
      Type Parameters:
      R - return type of each task and, it follows, the type of each item in the returned array
      Parameters:
      returnValueArrayCreator - creator of the array of Future task results to be returned
      futures - futures associated with forked tasks
      Returns:
      Throws:
      InterruptedException - if the current thread was interrupted while waiting
      ExecutionException - if the task associated with the future threw an exception
    • join

      @SafeVarargs public static <R> List<R> join(Future<R> a, Future<R> b, Future<R>... cToZ) throws InterruptedException, ExecutionException
      Blocks, if necessary, for the tasks associated with the futures to complete and returns the tasks' results as a List, preserving their order.
      Type Parameters:
      R - return type of each task and, it follows, the type of each element in the returned List
      Parameters:
      a -
      b -
      cToZ -
      Returns:
      Throws:
      InterruptedException - if the current thread was interrupted while waiting
      ExecutionException - if the task associated with the future threw an exception
    • join

      @SafeVarargs public static <R> R[] join(IntFunction<R[]> returnValueArrayCreator, Future<R> a, Future<R> b, Future<R>... cToZ) throws InterruptedException, ExecutionException
      Blocks, if necessary, for the tasks associated with the futures to complete and returns the tasks' results as an array, preserving their order, starting with the result from a, then from b, then from cToZ in order.
      Type Parameters:
      R - return type of each task and, it follows, the type of each item in the returned array
      Parameters:
      returnValueArrayCreator - creator of the array of Future task results to be returned
      a -
      b -
      cToZ -
      Returns:
      Throws:
      InterruptedException - if the current thread was interrupted while waiting
      ExecutionException - if the task associated with the future threw an exception
    • join

      public static <R> List<R> join(Collection<Future<R>> futures) throws InterruptedException, ExecutionException
      Blocks, if necessary, for the tasks associated with the futures to complete and returns the tasks' results as a List, preserving their order.
      Type Parameters:
      R - return type of each task and, it follows, the type of each element in the returned List
      Parameters:
      futures - futures associated with forked tasks
      Returns:
      Throws:
      InterruptedException - if the current thread was interrupted while waiting
      ExecutionException - if the task associated with the future threw an exception
    • fork_loop

      public static <R> Future<R>[] fork_loop(int min, int maxExclusive, TaskIntFunction<R> intFunction)
      Forks a value returning task for each index in the range [min, maxExclusive). The index will be passed to the task function. This method does not block. The returned array of futures will be in the natural order. For example: The future for the min index task will be located in 0th index of the returned array. The future for the maxExclusive-1 index task will be located in the index of the returned array.
      Type Parameters:
      R - the return type of the intFunction, and, it follows, the generic type of each Future.
      Parameters:
      min - the inclusive minimum of the range to create tasks
      maxExclusive - the exclusive maximum of the range to create tasks
      intFunction - function which represents the work for each task, parameterized by its index
      Returns:
      an array of Futures associated with each forked task, in natural order
      See Also:
    • void_fork_loop

      public static Future<Void>[] void_fork_loop(int min, int maxExclusive, TaskIntConsumer intConsumer)
      Forks a void returning task for each index in the range [min, maxExclusive). The index will be passed to the task function. This method does not block.
      Parameters:
      min -
      maxExclusive -
      intConsumer -
      Returns:
      See Also:
    • fork_loop

      public static <T, R> Future<R>[] fork_loop(T[] array, TaskFunction<T,R> function)
      Forks a value returning task for each item in the specified array. The item will be passed to the task function. This method does not block.
      Type Parameters:
      T - the type of the input array, and, it follows, the type of the parameter accepted by the task function
      R - the return type of the function, and, it follows, the generic type of each Future.
      Parameters:
      array - the input array
      function - the value returning task
      Returns:
      an array of Futures associated with each forked task, preserving the order of the input array
      See Also:
    • void_fork_loop

      public static <T> Future<Void>[] void_fork_loop(T[] array, TaskConsumer<T> consumer)
      Forks a void returning task for each item in the specified array. The item will be passed to the task function. This method does not block.
      Type Parameters:
      T - the type of the input array, and, it follows, the type of the parameter accepted by the task function
      Parameters:
      array - the input array
      consumer - the void returning task
      Returns:
      an array of Futures associated with each forked task, preserving the order of the input array
      See Also:
    • fork_loop

      public static <T, R> List<Future<R>> fork_loop(Iterable<T> iterable, TaskFunction<T,R> function)
      Forks a value returning task for each element in the specified Iterable. The element will be passed to the task function. This method does not block.
      Type Parameters:
      T - the type of the input iterable, and, it follows, the type of the parameter accepted by the task function
      R - the return type of the function, and, it follows, the generic type of each Future.
      Parameters:
      iterable - the input iterable
      function - the value returning task
      Returns:
      a List of Futures associated with each forked task, preserving the order of the input iterable
      See Also:
    • void_fork_loop

      public static <T> List<Future<Void>> void_fork_loop(Iterable<T> iterable, TaskConsumer<T> consumer)
      Forks a void returning task for each element in the specified Iterable. The element will be passed to the task function. This method does not block.
      Type Parameters:
      T - the type of the input iterable, and, it follows, the type of the parameter accepted by the task function
      Parameters:
      iterable - the input iterable
      consumer - the void returning task
      Returns:
      a List of Futures associated with each forked task, preserving the order of the input iterable
      See Also:
    • fork_loop_with_index

      public static <T, R> Future<R>[] fork_loop_with_index(T[] array, TaskFunctionWithIndex<T,R> functionWithIndex)
      Forks a value returning task for each item in the specified array. The item along with its index in the array will be passed to the task function. This method does not block.
      Type Parameters:
      T - the type of the input array, and, it follows, the type of the first parameter accepted by the task function
      R - the return type of the function, and, it follows, the generic type of each Future.
      Parameters:
      array - the input array
      functionWithIndex - the value returning task
      Returns:
      an array of Futures associated with each forked task, preserving the order of the input array
    • void_fork_loop_with_index

      public static <T> Future<Void>[] void_fork_loop_with_index(T[] array, TaskConsumerWithIndex<T> consumerWithIndex)
      Forks a void returning task for each item in the specified array. The item along with its index in the array will be passed to the task function. This method does not block.
      Type Parameters:
      T - the type of the input array, and, it follows, the type of the first parameter accepted by the task function
      Parameters:
      array - the input array
      consumerWithIndex - the void returning task
      Returns:
      an array of Futures associated with each forked task, preserving the order of the input array
      See Also:
    • fork_loop_with_index

      public static <T, R> List<Future<R>> fork_loop_with_index(Iterable<T> iterable, TaskFunctionWithIndex<T,R> functionWithIndex)
      Forks a value returning task for each item in the specified iterable. The element along with its index in the iterable will be passed to the task function. This method does not block.
      Type Parameters:
      T - the type of the input iterable, and, it follows, the type of the first parameter accepted by the task function
      R - the return type of the function, and, it follows, the generic type of each Future.
      Parameters:
      iterable - the input iterable
      functionWithIndex - the value returning task
      Returns:
      a List of Futures associated with each forked task, preserving the order of the input iterable
      See Also:
    • void_fork_loop_with_index

      public static <T> List<Future<Void>> void_fork_loop_with_index(Iterable<T> iterable, TaskConsumerWithIndex<T> consumerWithIndex)
      Type Parameters:
      T - the type of the input iterable, and, it follows, the type of the parameter accepted by the task function
      Parameters:
      iterable - the input iterable
      consumerWithIndex - the void returning task
      Returns:
      a List of Futures associated with each forked task, preserving the order of the input iterable
      See Also:
    • join_fork_loop

      public static <R> R[] join_fork_loop(IntFunction<R[]> returnValueArrayCreator, int min, int maxExclusive, TaskIntFunction<R> intFunction) throws InterruptedException, ExecutionException
      Blocks, if necessary, for the tasks associated with the fork_loop to complete.
      Type Parameters:
      R -
      Parameters:
      returnValueArrayCreator -
      min -
      maxExclusive -
      intFunction -
      Returns:
      Throws:
      InterruptedException - if the current thread was interrupted while waiting
      ExecutionException - if the task associated with the future threw an exception
      See Also:
    • join_fork_loop

      public static <R> List<R> join_fork_loop(int min, int maxExclusive, TaskIntFunction<R> intFunction) throws InterruptedException, ExecutionException
      Blocks, if necessary, for the tasks associated with the fork_loop to complete.
      Type Parameters:
      R -
      Parameters:
      min -
      maxExclusive -
      intFunction -
      Returns:
      Throws:
      InterruptedException - if the current thread was interrupted while waiting
      ExecutionException - if the task associated with the future threw an exception
      See Also:
    • join_fork_loop

      public static <T, R> R[] join_fork_loop(IntFunction<R[]> returnValueArrayCreator, T[] array, TaskFunction<T,R> function) throws InterruptedException, ExecutionException
      Blocks, if necessary, for the tasks associated with the fork_loop to complete.
      Type Parameters:
      T -
      R -
      Parameters:
      returnValueArrayCreator -
      array -
      function -
      Returns:
      Throws:
      InterruptedException - if the current thread was interrupted while waiting
      ExecutionException - if the task associated with the future threw an exception
      See Also:
    • join_fork_loop

      public static <T, R> List<R> join_fork_loop(T[] array, TaskFunction<T,R> function) throws InterruptedException, ExecutionException
      Blocks, if necessary, for the tasks associated with the fork_loop to complete.
      Type Parameters:
      T -
      R -
      Parameters:
      array -
      function -
      Returns:
      Throws:
      InterruptedException - if the current thread was interrupted while waiting
      ExecutionException - if the task associated with the future threw an exception
      See Also:
    • join_fork_loop

      public static <T, R> List<R> join_fork_loop(Iterable<T> iterable, TaskFunction<T,R> function) throws InterruptedException, ExecutionException
      Blocks, if necessary, for the tasks associated with the fork_loop to complete.
      Type Parameters:
      T -
      R -
      Parameters:
      iterable -
      function -
      Returns:
      Throws:
      InterruptedException - if the current thread was interrupted while waiting
      ExecutionException - if the task associated with the future threw an exception
      See Also:
    • join_fork_loop_with_index

      public static <T, R> List<R> join_fork_loop_with_index(Iterable<T> iterable, TaskFunctionWithIndex<T,R> functionWithIndex) throws InterruptedException, ExecutionException
      Blocks, if necessary, for the tasks associated with the fork_loop to complete.
      Type Parameters:
      T -
      R -
      Parameters:
      iterable -
      functionWithIndex -
      Returns:
      Throws:
      InterruptedException - if the current thread was interrupted while waiting
      ExecutionException - if the task associated with the future threw an exception
      See Also:
    • join_fork_loop_with_index

      public static <T, R> List<R> join_fork_loop_with_index(T[] array, TaskFunctionWithIndex<T,R> functionWithIndex) throws InterruptedException, ExecutionException
      Blocks, if necessary, for the tasks associated with the fork_loop to complete.
      Type Parameters:
      T -
      R -
      Parameters:
      array -
      functionWithIndex -
      Returns:
      Throws:
      InterruptedException - if the current thread was interrupted while waiting
      ExecutionException - if the task associated with the future threw an exception
      See Also:
    • join_fork_loop_with_index

      public static <T, R> R[] join_fork_loop_with_index(IntFunction<R[]> returnValueArrayCreator, T[] array, TaskFunctionWithIndex<T,R> functionWithIndex) throws InterruptedException, ExecutionException
      Blocks, if necessary, for the tasks associated with the fork_loop to complete.
      Type Parameters:
      T -
      R -
      Parameters:
      returnValueArrayCreator -
      array -
      functionWithIndex -
      Returns:
      Throws:
      InterruptedException - if the current thread was interrupted while waiting
      ExecutionException - if the task associated with the future threw an exception
      See Also:
    • join_void_fork_loop

      public static void join_void_fork_loop(int min, int maxExclusive, TaskIntConsumer consumer) throws InterruptedException, ExecutionException
      Blocks, if necessary, for the tasks associated with the fork_loop to complete.
      Parameters:
      min -
      maxExclusive -
      consumer -
      Throws:
      InterruptedException - if the current thread was interrupted while waiting
      ExecutionException - if the task associated with the future threw an exception
      See Also:
    • join_void_fork_loop

      public static <T> void join_void_fork_loop(T[] array, TaskConsumer<T> consumer) throws InterruptedException, ExecutionException
      Blocks, if necessary, for the tasks associated with the fork_loop to complete.
      Type Parameters:
      T -
      Parameters:
      array -
      consumer -
      Throws:
      InterruptedException - if the current thread was interrupted while waiting
      ExecutionException - if the task associated with the future threw an exception
      See Also:
    • join_void_fork_loop

      public static <T> void join_void_fork_loop(Iterable<T> iterable, TaskConsumer<T> consumer) throws InterruptedException, ExecutionException
      Blocks, if necessary, for the tasks associated with the fork_loop to complete.
      Type Parameters:
      T -
      Parameters:
      iterable -
      consumer -
      Throws:
      InterruptedException - if the current thread was interrupted while waiting
      ExecutionException - if the task associated with the future threw an exception
      See Also:
    • join_void_fork_loop_with_index

      public static <T> void join_void_fork_loop_with_index(Iterable<T> iterable, TaskConsumerWithIndex<T> consumerWithIndex) throws InterruptedException, ExecutionException
      Blocks, if necessary, for the tasks associated with the fork_loop to complete.
      Type Parameters:
      T -
      Parameters:
      iterable -
      consumerWithIndex -
      Throws:
      InterruptedException - if the current thread was interrupted while waiting
      ExecutionException - if the task associated with the future threw an exception
      See Also:
    • join_void_fork_loop_with_index

      public static <T> void join_void_fork_loop_with_index(T[] array, TaskConsumerWithIndex<T> consumerWithIndex) throws InterruptedException, ExecutionException
      Blocks, if necessary, for the tasks associated with the fork_loop to complete.
      Type Parameters:
      T -
      Parameters:
      array -
      consumerWithIndex -
      Throws:
      InterruptedException - if the current thread was interrupted while waiting
      ExecutionException - if the task associated with the future threw an exception
      See Also:
    • join_void_fork_loop_auto_coarsen

      public static void join_void_fork_loop_auto_coarsen(int min, int maxExclusive, TaskIntConsumer consumer) throws InterruptedException, ExecutionException
      Throws:
      InterruptedException
      ExecutionException
    • join_void_fork_loop_2d

      public static void join_void_fork_loop_2d(int aMin, int aMaxExclusive, int bMin, int bMaxExclusive, TaskBiIntConsumer biConsumer) throws InterruptedException, ExecutionException
      Throws:
      InterruptedException
      ExecutionException
    • join_void_fork_loop_2d_auto_coarsen

      public static void join_void_fork_loop_2d_auto_coarsen(int aMin, int aMaxExclusive, int bMin, int bMaxExclusive, TaskBiIntConsumer biConsumer) throws InterruptedException, ExecutionException
      Throws:
      InterruptedException
      ExecutionException
    • getForkJoin

      public static ForkJoin getForkJoin()
    • setForkJoin

      public static ForkJoin setForkJoin(ForkJoin forkJoin)