I have an application which uses a separate thread to execute a calculation performed in code from a separate library. I would like the user to able to cancel this operation as, depending on the parameters, it can take a long time.
I cannot use a stopping variable or check for interrupts as the calculation from the applications point of view is a single line
public void run() {
result = library.performCalculation(params);
}
I would like to be able to use thread.stop() but as far as I’m aware this is not posible on android.
Is there anyway I can cause the thread to stop running?
You’re correct,
Thread.stop()is not available on Android (and even on systems where it is still supported, its problems outweight its usefulness).What you can do is two-fold. First, call
thread.interrupt()to set an interrupted flag on the thread running your library. Second, modify your library at appropriate point(s) to callisInterrupted()to determine if it is interrupted and, if so, it should voluntarily clean up and leave.