Class TaskUtil

java.lang.Object
eu.cloudnetservice.utils.base.concurrent.TaskUtil

@Internal public final class TaskUtil extends Object
A Future util, which provides utility methods to supply runnables and functions into a task. Furthermore, the util provides methods to await a task without throwing any exception.
Since:
4.0
  • Field Details

    • VIRTUAL_THREAD_EXECUTOR

      private static final ExecutorService VIRTUAL_THREAD_EXECUTOR
  • Constructor Details

    • TaskUtil

      private TaskUtil()
  • Method Details

    • runAsync

      Runs the given runnable in a fork thread pool and wraps it into a completable future.
      Parameters:
      runnable - the runnable to run.
      Returns:
      a new completable future containing the given runnable.
      Throws:
      NullPointerException - if the given runnable is null.
    • runVirtualAsync

      @NonNull public static @NonNull CompletableFuture<Void> runVirtualAsync(@NonNull @NonNull CheckedRunnable runnable)
      Runs the given runnable in a virtual thread pool and wraps it into a completable future.
      Parameters:
      runnable - the runnable to run.
      Returns:
      a new completable future containing the given runnable.
      Throws:
      NullPointerException - if the given runnable is null.
    • supplyAsync

      @NonNull public static <V> @NonNull CompletableFuture<V> supplyAsync(@NonNull @NonNull CheckedFunction0<V> supplier)
      Supplies the given supplier into the fork thread pool. A new future is created and completed with the return value of the supplier. Thrown exceptions are caught and passed into the created future.
      Type Parameters:
      V - the generic type of the supplier and the future.
      Parameters:
      supplier - the supplier to execute in the thread pool.
      Returns:
      the new future completing the value of the supplier.
      Throws:
      NullPointerException - if the given supplier is null.
    • supplyVirtualAsync

      @NonNull public static <V> @NonNull CompletableFuture<V> supplyVirtualAsync(@NonNull @NonNull CheckedFunction0<V> supplier)
      Supplies the given supplier into a virtual thread pool. A new future is created and completed with the return value of the supplier. Thrown exceptions are caught and passed into the created future.
      Type Parameters:
      V - the generic type of the supplier and the future.
      Parameters:
      supplier - the supplier to execute in the thread pool.
      Returns:
      the new future completing the value of the supplier.
      Throws:
      NullPointerException - if the given supplier is null.
    • finishedFuture

      @NonNull public static <V> @NonNull CompletableFuture<V> finishedFuture(@Nullable @Nullable Object result)
      Creates a new task that already has a defined result. If the given result is a throwable the task is completed exceptionally CompletableFuture.failedFuture(Throwable) instead of completing a normal result.
      Type Parameters:
      V - the generic type of the new task.
      Parameters:
      result - the result for the new task.
      Returns:
      the new task completed with the given result.
    • getOrDefault

      public static <V> @UnknownNullability V getOrDefault(@NonNull @NonNull Future<V> future, @Nullable V defaultValue)
      This blocks the calling thread until the result of the task is available. If any exception occurs during the execution of the future or the future is cancelled the given default value is returned.
      Type Parameters:
      V - the generic type of the future and the result.
      Parameters:
      future - the future to await.
      defaultValue - the default value returned on failure.
      Returns:
      the completed result of this future or the default value if any exception occurred.
    • getOrDefault

      public static <V> @UnknownNullability V getOrDefault(@NonNull @NonNull Future<V> future, @NonNull @NonNull Duration timeout, @Nullable V defaultValue)
      This blocks the calling thread until the result of the future is available. If any exception occurs during the execution of the future or the future is cancelled the given default value is returned. This will also return the default value if the future did not complete the result within the given time-out.

      If the given timeout is either Duration.ZERO or a negative duration no timeout is used.

      Type Parameters:
      V - the generic type of the future and the result.
      Parameters:
      future - the future to await.
      timeout - the duration to wait for the completion before throwing an exception.
      defaultValue - the default value returned on failure or if the future did not complete within time-out.
      Returns:
      the completed result of this future or the default value if the future did not complete.
      Throws:
      NullPointerException - if the given future or timeout is null.