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 Summary
Modifier and TypeMethodDescriptiondefault <T> TInvoke the passed in task on the calling thread, taking any required locks or other resources.voidinvokeLater(Runnable task) Execute a task on the Root thread.booleanCheck whether the current thread is the active thread executing inside a Root update.booleanCheck whether the current thread is the thread currently driving a Root implementation.default booleanCheck whether the ThreadContext implementation supports direct invocation usinginvoke(java.util.concurrent.Callable).
-
Method Details
-
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 thanisInUpdate()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
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 usinginvoke(java.util.concurrent.Callable). The default implementation returns false.- Returns:
- true if direct invocation is supported
-
invoke
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 viasupportsDirectInvoke().The default implementation throws
UnsupportedOperationException, even whenisInUpdate()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 supportedException- from execution of underlying task
-