core.utilities / AffinityExecutor

# AffinityExecutor

`interface AffinityExecutor : Executor`

An extended executor interface that supports thread affinity assertions and short circuiting. This can be useful for ensuring code runs on the right thread, and also for unit testing.

### Types

Gate | `class Gate : AffinityExecutor` An executor useful for unit tests: allows the current thread to block until a command arrives from another thread, which is then executed. Inbound closures/commands stack up until they are cleared by looping.
ServiceAffinityExecutor | `class ServiceAffinityExecutor : AffinityExecutor, ThreadPoolExecutor` An executor backed by thread pool (which may often have a single thread) which makes it easy to schedule tasks in the future and verify code is running on the executor.

### Properties

isOnThread | `abstract val isOnThread: Boolean` Returns true if the current thread is equal to the thread this executor is backed by.

### Functions

checkOnThread | `open fun checkOnThread(): Unit` Throws an IllegalStateException if the current thread is not one of the threads this executor is backed by.
executeASAP | `open fun executeASAP(runnable: () -> Unit): Unit` If isOnThread() then runnable is invoked immediately, otherwise the closure is queued onto the backing thread.
fetchFrom | `open fun <T> fetchFrom(fetcher: () -> T): T` Runs the given function on the executor, blocking until the result is available. Be careful not to deadlock this way Make sure the executor cant possibly be waiting for the calling thread.
flush | `open fun flush(): Unit` Posts a no-op task to the executor and blocks this thread waiting for it to complete. This can be useful in tests when you want to be sure that a previous task submitted via execute has completed.

### Companion Object Properties

SAME_THREAD | `val SAME_THREAD: AffinityExecutor`

### Inheritors

Gate | `class Gate : AffinityExecutor` An executor useful for unit tests: allows the current thread to block until a command arrives from another thread, which is then executed. Inbound closures/commands stack up until they are cleared by looping.
ServiceAffinityExecutor | `class ServiceAffinityExecutor : AffinityExecutor, ThreadPoolExecutor` An executor backed by thread pool (which may often have a single thread) which makes it easy to schedule tasks in the future and verify code is running on the executor.