Interface ThreadContext


  • public interface ThreadContext
    An optional context available from the Root lookup, providing the ability to query whether code is running on the Root thread, and to pass tasks to invoke on the Root thread.
    • Method Detail

      • isInUpdate

        boolean isInUpdate()
        Check whether the current thread is the active thread executing inside a Root update.
        Returns:
        current thread is in Root update
      • isRootThread

        boolean isRootThread()
        Check whether the current thread is the thread currently driving a Root implementation. This is a looser check than isInUpdate() where a Root implementation may be driven by an external context - eg. UI event thread. It will always return true when isInUpdate() returns true. This method does not return true if a thread has been returned to a pool. As a guide, it will return true if blocking the current thread would block the Root from executing.
        Returns:
        current thread is Root executing thread
      • invokeLater

        void invokeLater​(Runnable task)
        Execute a task on the Root thread. The task should carefully check that its state is still valid - eg. a component should check that it has not been removed from the Root hierarchy.
        Parameters:
        task - task to be executed
      • supportsDirectInvoke

        default boolean supportsDirectInvoke()
        Check whether the ThreadContext implementation supports direct invocation using invoke(java.util.concurrent.Callable). The default implementation returns false.
        Returns:
        true if direct invocation is supported
      • invoke

        default <T> T invoke​(Callable<T> task)
                      throws Exception
        Invoke the passed in task on the calling thread, taking any required locks or other resources. This method should be used with caution when absolutely necessary. Callers should check the method is supported via supportsDirectInvoke().

        The default implementation throws UnsupportedOperationException, even when isInUpdate() is true and it is therefore safe for the caller to directly execute the task.

        Type Parameters:
        T - generic type of task
        Parameters:
        task - task to invoke
        Returns:
        result of task
        Throws:
        UnsupportedOperationException - if direct invoke not supported
        Exception - from execution of underlying task