6.2 Running a Simple Threadable Task

Implementing a long running threadable task is just an exercise in implementing the run method of the Threadable interface. Unlike code which runs in the context of the main reactor thread - such as timer, signal and deferrable callbacks - threadable tasks are allowed to block. In the example shown in Listing 6.3 there is a blocking call to the Thread.sleep method which halts the execution of the threadable task for 1 second.


\begin{listing}
% latex2html id marker 1681\begin{small}\begin{verbatim}publ...
...rbatim} \end{small}\caption{A Simple Threadable Long Running Task}
\end{listing}

Assuming that the long running task is not interrupted while sleeping, it will return a generically typed data object as specified by the interface definition for the run method. In order to run the threadable task, the runThread method on the Reactor interface is invoked as shown in Listing 6.4.


\begin{listing}
% latex2html id marker 1693\begin{small}\begin{verbatim}...
...
...batim} \end{small}\caption{Running a Threadable Long Running Task}
\end{listing}

The runThread method returns a deferred event object which will have its callbacks executed when the threadable task's run method returns. The callback parameter passed up the callback chain will be the generically typed data object which is returned by the threadable task's run method. In order to process the callbacks, the simple deferrable callback handler previously presented in Section 5.2 may be used. The full implementation of this example may be found in the thread examples package as ThreadableExample1.