6.5 Cancelling a Running Threadable Task

The process of cancelling a running threadable task makes use of the underlying Java threads API for interrupting blocked threads. When the reactor's cancelThread method is called for a given threadable task object, the standard interrupt method is called on the underlying Java thread. This means that the blocked thread will be interrupted, throwing an exception of type InterruptedException. This exception will then be passed back via the threadable's deferred errback chain using the standard deferred error handling model.


\begin{listing}
% latex2html id marker 1760\begin{small}\begin{verbatim}...
...
... \end{small}\caption{Cancellation of Threadable Long Running Task}
\end{listing}

The cancellation of threadable tasks may be demonstrated by making a small modification to the simple threadable test example previously used. The modified thread startup and cancellation code is shown in Listing 6.9. This results in the console output given in Listing 6.10, which clearly demonstrates the generation of the InterruptedException errback. The full version of this example is included in the thread examples package as ThreadableCancelExample.


\begin{listing}
% latex2html id marker 1773\begin{small}\begin{verbatim}INFO...
...im} \end{small}\caption{Console Output of Threadable Cancellation}
\end{listing}