Variation of
Runnable that allows other threads to give a hint to the
{
Runnable.run() method that it should terminate.
This is useful mainly with implementations of
run() that don't finish immediately.
Note that in Java
Thread.stop() and similar methods are deprecated, and that
the proper way to terminate asynchronously running code is to signal the termination request
via some variable that the thread is supposed to check at convenient points.
Therefore if your run method takes non-negligible time, you should
- provide a subclass of this
CancelableRunnable as the loop action in
ThreadLoopRunner#ThreadLoopRunner(ThreadFactory, Runnable, Logger)
- implement
run() to check at some points whether the flag shouldTerminate
has been set (e.g. by ThreadLoopRunner.shutdown(long, java.util.concurrent.TimeUnit) calling cancel()),
and if so, to return from the run method as quickly as possible, but yet cleaning up.