6.7 Execution Context

Threadable tasks may be scheduled for execution using the reactor's runThread method from any thread context. Similarly, the reactor's cancelThread method may be used to cancel a running threadable task from any context. All deferred callback processing associated with the completion of a threadable task is carried out within the context of the main reactor thread.

Threadable tasks run within independent worker threads, so the usual synchronisation issues associated with sharing data between threads still need to be considered. Thread synchronisation can be much simplified by using immutable objects for the threadable input data and return value. An alternative model is to effectively transfer ownership of the threadable input data to the threadable object by discarding references to it in the initiating thread after it has been passed as the data parameter to the runThread method.

Each time a threadable task is scheduled for execution by calling the reactor's runThread method it will be arbitrarily assigned to an existing Java thread from the reactor's thread pool. If a stateful threadable task is resubmitted for execution, there is no guarantee that successive runs will use the same underlying Java thread object. This means that threadable tasks must not attempt to directly manipulate the underlying Java thread state.