title: Stopping the execution of a Groovy script
body: <p>I am embeding Groovy runtime in my code and I would like to have the ability to interrupt it. I don't have control of the scripts that are going to run. I read about groovy.transform.ThreadInterrupt to handle thread interruptions but for some reason this code below isn't working as intended. It's actually waiting 10000 ms instead of the 1000 where it should get interrupted.</p>
  
  <p>Any ideas? Thank you.</p>
  
  <pre><code>import groovy.lang.Binding;
  import groovy.lang.GroovyShell;
  import groovy.transform.ThreadInterrupt;
  import org.codehaus.groovy.control.CompilerConfiguration;
  import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer;
  
  public class GroovyTest extends Thread {
      private Binding binding;
      private GroovyShell shell;
  
      public GroovyTest() {
          CompilerConfiguration compilerConfig = new CompilerConfiguration();
          compilerConfig.addCompilationCustomizers(
                  new ASTTransformationCustomizer(ThreadInterrupt.class));
  
          binding = new Binding();
  
          shell = new GroovyShell(this.getClass().getClassLoader(), binding, compilerConfig);
      }
  
      @Override
      public void run() {
          System.out.println("Started");
  
          shell.run("for(int i = 0; i &lt; 10; i++) {sleep(1000)}", "test", new String[] {});
  
          System.out.println("Finished");
      }
  
      public static void main(String args[]) throws InterruptedException {
          GroovyTest test = new GroovyTest();
  
          test.start();
  
          System.out.println("Sleeping: " + System.currentTimeMillis());
  
          Thread.sleep(1000);
  
          System.out.println("Interrupting: " + System.currentTimeMillis());
  
          test.interrupt();
          test.join();
  
          System.out.println("Interrupted?: " + System.currentTimeMillis());
      }
  }
  </code></pre>