6.4 Stateful Threadable Task Objects

A useful feature of threadable task objects is that it is possible to create stateful threadable tasks which may be submitted for execution multiple times. The one constraint is that a given threadable task cannot be resubmitted if the task is already running. Any attempt to resubmit a threadable task that is already running will cause the reactor's runThread method to throw a runtime exception of type ThreadableRunningException. An example implementation of a stateful threadable task is shown in Listing 6.7.


\begin{listing}
% latex2html id marker 1733\begin{small}\begin{verbatim}publ...
...atim} \end{small}\caption{A Stateful Threadable Long Running Task}
\end{listing}

In the example the task state is represented by the count member variable. This will be incremented each time the threadable task is submitted for execution, until an exception is thrown on the fifth execution of the run method. The corresponding deferrable callback handler used in the example is shown in Listing 6.8. The onCallback method automatically resubmits the threadable task object for execution and the onErrback method terminates the test on completion. The full implementation of this example is included in the thread examples package as ThreadableResubmitExample.


\begin{listing}
% latex2html id marker 1748\begin{small}\begin{verbatim}publ...
...small}\caption{Resubmitting Threadable From a Deferrable Callback}
\end{listing}