Interface WorkMonitor


public interface WorkMonitor
Monitors "work" associated with a computer, keeping track of how much a computer has done, and ensuring every computer receives a fair share of any processing time.

This is primarily intended for work done by peripherals on the main thread (such as on a block entity's tick), but could be used for other purposes (such as complex computations done on another thread).

Before running a task, one should call canWork() to determine if the computer is currently allowed to execute work. If that returns true, you should execute the task and use trackWork(long, TimeUnit) to inform the monitor how long that task took.

Alternatively, use runWork(Runnable) to run and keep track of work.

See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    If the owning computer is currently allowed to execute work.
    default boolean
    runWork(Runnable runnable)
    Run a task if possible, and inform the monitor of how long it took.
    boolean
    If the owning computer is currently allowed to execute work, and has ample time to do so.
    void
    trackWork(long time, TimeUnit unit)
    Inform the monitor how long some piece of work took to execute.
  • Method Details

    • canWork

      boolean canWork()
      If the owning computer is currently allowed to execute work.
      Returns:
      If we can execute work right now.
    • shouldWork

      boolean shouldWork()
      If the owning computer is currently allowed to execute work, and has ample time to do so.

      This is effectively a more restrictive form of canWork(). One should use that in order to determine if you may do an initial piece of work, and shouldWork to determine if any additional task may be performed.

      Returns:
      If we should execute work right now.
    • trackWork

      void trackWork(long time, TimeUnit unit)
      Inform the monitor how long some piece of work took to execute.
      Parameters:
      time - The time some task took to run
      unit - The unit that time was measured in.
    • runWork

      default boolean runWork(Runnable runnable)
      Run a task if possible, and inform the monitor of how long it took.
      Parameters:
      runnable - The task to run.
      Returns:
      If the task was actually run (namely, canWork() returned true).