Class DelayedTask

java.lang.Object
civitas.celestis.task.DelayedTask
All Implemented Interfaces:
Task

public class DelayedTask extends Object implements Task
A delayed task which is only meant to be executed once. No matter how many schedulers this task is registered to, it is guaranteed that it will only be executed once throughout its lifetime.

Delayed tasks can be extended, but is not recommended. The intended way of using a delayed task is by calling one of its constructors and providing either a Runnable or a Consumer. The latter will be provided with the task's delta. (the actual time it took between registration and execution in milliseconds)

See Also:
  • Constructor Details

    • DelayedTask

      public DelayedTask(@Nonnull Runnable action, long delay)
      Creates a new delayed task.
      Parameters:
      action - The action to execute when the delay has passed
      delay - The delay of this task in milliseconds
    • DelayedTask

      public DelayedTask(@Nonnull Consumer<Long> action, long delay)
      Creates a new delayed task.
      Parameters:
      action - The action to execute when the delay has passed
      delay - The delay of this task in milliseconds
  • Method Details

    • execute

      public final void execute(long delta)
      Handles the execution of this task. It first checks if this task has already been executed. If it has not been executed, it executed the stored action, then sets the executed flag to true. If not, it simply continues. It then proceeds to unregister itself from all registered schedulers, as it no longer requires to be registered to any scheduler. (it will do nothing)
      Specified by:
      execute in interface Task
      Parameters:
      delta - The duration between the last execution and now in milliseconds
    • interval

      public final long interval()
      The interval will serve as the delay of this task.
      Specified by:
      interval in interface Task
      Returns:
      The delay of this task in milliseconds
    • onRegistered

      public final void onRegistered(@Nonnull Scheduler scheduler)
      Adds a reference to the scheduler to the internal set of schedulers.
      Specified by:
      onRegistered in interface Task
      Parameters:
      scheduler - The scheduler this task was registered to
    • onUnregistered

      public final void onUnregistered(@Nonnull Scheduler scheduler)
      Removes a reference to the scheduler from the internal set of schedulers.
      Specified by:
      onUnregistered in interface Task
      Parameters:
      scheduler - The scheduler this task was unregistered from