DelayedSchedular - MeAlam1/BlueLib GitHub Wiki


DelayedScheduler

Overview

The DelayedScheduler class provides a simple and thread-safe way to schedule delayed asynchronous tasks using Java’s CompletableFuture API. This utility is ideal for executing Runnable or Supplier tasks after a specified delay without blocking the main thread.

It is designed to streamline delayed task execution across your mod in a reliable and concise manner.

Getting Started

Scheduling Methods

To begin using DelayedScheduler, call one of its static schedule methods to run a task after a given delay. The return value is a CompletableFuture, allowing you to chain further asynchronous logic.

Below is a breakdown of the available methods and their functionality.

Key Methods

  1. schedule(Runnable task, long delay, TimeUnit unit) Schedules a Runnable to be executed after the specified delay.

    DelayedScheduler.schedule(() -> System.out.println("Delayed Task"), 1, TimeUnit.SECONDS);
  2. schedule(Supplier supplier, long delay, TimeUnit unit) Schedules a Supplier to be executed after the specified delay and returns a result.

    CompletableFuture<String> future = DelayedScheduler.schedule(() -> "Hello after delay", 2, TimeUnit.SECONDS);

Usage Notes

  • Threading: Internally uses a single-threaded ScheduledExecutorService to ensure lightweight and sequential execution of tasks.
  • CompletableFuture: Returned futures allow chaining with .thenRun, .thenAccept, or .thenApply.
  • Error Handling: Exceptions thrown during task execution are captured and passed to the future via .completeExceptionally.
  • Shutdown Note: The executor service is designed to run as long as the application runs. Manual shutdown is not required for normal usage.

⚠️ **GitHub.com Fallback** ⚠️