1

While creating an app I have run into the issue of closing a thread that runs user inputted code using Beanshell. thread.stop(); failed to kill the thread any ideas? Running the code a line at a time isn't plausible due to loops in the users code.

final Thread thread = new Thread() {
    public void run() {
        try {
            interpreter.run();
        } catch (RuntimeException runEx) {
            runEx.printStackTrace();
            Log.d("END OF BSH", "END OF BSH");
        }
    }
};
thread.start();
thread.interrupt();
Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
  • You need to figure out how to tell the interpreter to terminate, or modify it to support that if it does not already. You cannot force-terminate threads. – Chris Stratton Apr 29 '15 at 15:23
  • Thread.interrupt(); only signals the thread that it should stop, it's the thread itself which must then manage it with if (Thread.interrupted()) . – Wildcopper Apr 29 '15 at 15:25
  • 2
    A very bad idea would be to run this in a service set by the manifest to be its own process, so that if necessary you can kill it. But that should really be considered an absolute last resort - Android doesn't like it when processes "randomly" die and under some circumstances will restart them. – Chris Stratton Apr 29 '15 at 15:31
  • Look at http://daniel.mitterdorfer.name/articles/2015/handling-interruptedexception/ for a fairly complete writeup about interrupt(), InterruptedException and friends. – llogiq Jun 10 '15 at 06:47

0 Answers0