Class RunOnce

java.lang.Object
com.tccc.kos.commons.util.concurrent.RunOnce

public class RunOnce extends Object
Runnable object that wraps a single function that will never be called concurrently and once returning true, will never be called again unless reset. This handles a common pattern when a startup process requires some operation to be run exactly once per boot or once per cycle of some process. This eliminates the need to track state and coordinate concurrency.
Since:
1.9
Version:
2025-10-26
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new RunOnce for the specified callback.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Run the callback in another thread.
    boolean
    Return true if the callback as been run successfully
    void
    Allow the callback to be run again.
    void
    run()
    Run the callback from the calling thread.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • RunOnce

      public RunOnce(RunOnce.RunOnceCallback callback)
      Create a new RunOnce for the specified callback. The callback can be triggered by calling run() to run it inline or execute() to run it on another thread. Once the callback returns true, it will never be called again unless reset() is called.
      Parameters:
      callback - the callback to run successfuly one time
  • Method Details

    • run

      public void run()
      Run the callback from the calling thread. If the callback is already running in another thread, this will return immediately and the callback will be queued to run again when the current callback ends.
    • execute

      public void execute()
      Run the callback in another thread.
    • reset

      public void reset()
      Allow the callback to be run again.
    • isDone

      public boolean isDone()
      Return true if the callback as been run successfully