-
public interface ThreadContext
An optional context available from theRoot
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
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default <T> T
invoke(Callable<T> task)
Invoke the passed in task on the calling thread, taking any required locks or other resources.void
invokeLater(Runnable task)
Execute a task on the Root thread.boolean
isInUpdate()
Check whether the current thread is the active thread executing inside a Root update.boolean
isRootThread()
Check whether the current thread is the thread currently driving a Root implementation.default boolean
supportsDirectInvoke()
Check whether the ThreadContext implementation supports direct invocation usinginvoke(java.util.concurrent.Callable)
.
-
-
-
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 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
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 usinginvoke(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 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
-
-