Class AbstractRoot.Delegate

  • Enclosing class:
    AbstractRoot

    protected abstract class AbstractRoot.Delegate
    extends Object
    An abstract delegate class that may be attached to this Root to drive it from another source (eg. audio callback or UI event queue). Subclasses should call doUpdate(long) regularly to update the clock time and process pending tasks.

    The delegate should prefer calling doTimedPoll(long, java.util.concurrent.TimeUnit) to thread sleeping or other timing mechanisms where possible so that incoming packets and tasks are processed while waiting.

    If the other source provides a mechanism for invoking tasks asynchronously (eg. EventQueue.invokeLater(Runnable task)) it should override onQueueReceipt() to asynchronously trigger doPollQueue() to process tasks and packets between updates.

    If the other source driving this delegate requires a new Thread to run on, it should obtain it from getThreadFactory()

    • Constructor Detail

      • Delegate

        protected Delegate()
        Create a Delegate with the default configuration.
      • Delegate

        protected Delegate​(AbstractRoot.DelegateConfiguration config)
        Create a Delegate subclass with the provided configuration.
        Parameters:
        config - delegate configuration
    • Method Detail

      • doUpdate

        protected final boolean doUpdate​(long time)
        Update the Root time and process tasks, inbound calls and clock listeners.
        Parameters:
        time - new clock time (directly from or related to RootHub.getClock()
        Returns:
        false if the Root has been terminated or the delegate detached
      • doPollQueue

        protected final void doPollQueue()
        Poll the queue, running any available tasks and dispatching any packets with a timecode before the current Root time.
      • doTimedPoll

        protected final void doTimedPoll​(long time,
                                         TimeUnit unit)
                                  throws InterruptedException
        Wait up to the given time for a queue notification, running any available tasks and dispatching any packets with a timecode before the current Root time.
        Parameters:
        time -
        unit -
        Throws:
        InterruptedException
      • getThreadFactory

        protected final ThreadFactory getThreadFactory()
        Get a thread factory for creating any new threads required by the delegate.
        Returns:
        thread factory for all required threads
      • isRootThread

        protected boolean isRootThread()
        Check whether the currently executing thread is the current thread executing the delegate. The AbstractRoot ThreadContext implementation will delegate to this method if the current thread is not inside an update. The default implementation checks whether the current thread is the last thread that called doUpdate(long) or doPollQueue(). Implementations may want to override this, eg. to check if the current thread is a UI update thread. As a guide, this method should return true if blocking the current thread would block the root from executing.
        Returns:
        current thread is root thread
        See Also:
        ThreadContext.isRootThread()