Class RunnableManager


  • public class RunnableManager
    extends java.lang.Object

    A utility class that is capable of maintaining the running state of a Runnable, i.e. frequent calls of the executeAsync(Runnable) with long running Runnables will not result in each Runnable being called. This is useful, for instance, if the same Runnable is submitted multiple times unnecessarily.

    If a runnable additionally implements the RunnableManager.BackgroundStage interface, then it will be invoked on a background thread and when complete will be asked to provide a subsequent stage to execute. This may similarly be a background stage, iterating the process, or may be a terminal runnable that then is posted on the UI thread to update the UI.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  RunnableManager.BackgroundStage
      An optional mix-in interface for a Runnable scheduled on the RunnableManager that should be run in a background thread and which produces a subsequent stage for further execution.
    • Constructor Summary

      Constructors 
      Constructor Description
      RunnableManager​(org.eclipse.swt.widgets.Display display)
      Constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void executeAsync​(java.lang.Runnable runnable)
      Execute the given Runnable via Display.asyncExec(Runnable).
      org.eclipse.swt.widgets.Display getDisplay()  
      boolean isRunning()
      Query whether a runnable is in progress (asynchronously) on the display thread.
      void waitForIdle()
      Wait until no task is running.
      boolean waitForIdle​(long time, java.util.concurrent.TimeUnit unit)
      Wait until no task is running or the given time elapses.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • RunnableManager

        public RunnableManager​(org.eclipse.swt.widgets.Display display)
        Constructor.
        Parameters:
        display - the Display that is used to submit a runnable
    • Method Detail

      • executeAsync

        public void executeAsync​(java.lang.Runnable runnable)
        Execute the given Runnable via Display.asyncExec(Runnable).
        Parameters:
        runnable - the Runnable to be executed asynchronously
      • getDisplay

        public org.eclipse.swt.widgets.Display getDisplay()
        Returns:
        the display
      • isRunning

        public boolean isRunning()
        Query whether a runnable is in progress (asynchronously) on the display thread. The implication is that posting a new runnable at this instant would be redundant.
        Returns:
        true if a runnable is currently running on the display thread or waiting to run on the display thread; false, otherwise
        Since:
        1.20
      • waitForIdle

        public final void waitForIdle()
                               throws java.lang.InterruptedException
        Wait until no task is running. Returns immediately if there is currently no task running.
        Throws:
        java.lang.InterruptedException - if interrupted while waiting
        Since:
        1.21
      • waitForIdle

        public final boolean waitForIdle​(long time,
                                         java.util.concurrent.TimeUnit unit)
                                  throws java.lang.InterruptedException
        Wait until no task is running or the given time elapses. Returns immediately if there is currently no task running. Note that if called on the UI thread, then the actual wait time in case of time-out can be longer than requested because of UI event queue processing.
        Parameters:
        time - the amount of time to wait, or a non-positive amount to wait indefinitely as in waitForIdle()
        unit - the unit of measure of the time to wait
        Returns:
        true if on return there is no task running; false on time-out (which does not mean that since the time-out occurred the manager did not become idle)
        Throws:
        java.lang.InterruptedException - if interrupted while waiting
        Since:
        1.21