停止执行Groovy脚本

时间:2011-08-12 04:31:50

标签: java multithreading groovy groovyshell

我在我的代码中嵌入了Groovy运行时,我希望能够中断它。我无法控制要运行的脚本。我读了关于groovy.transform.ThreadInterrupt来处理线程中断但由于某种原因,下面的代码没有按预期工作。它实际上等待的是10000毫秒而不是它应该被打断的1000毫秒。

有什么想法吗?谢谢。

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 < 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());
    }
}

1 个答案:

答案 0 :(得分:4)

回答我自己的问题。 即使你尝试没有闭包,Groovy的静态方法睡眠也不会中断。 如果你问我,很奇怪的默认。 推荐的方法是调用Thread.sleep(ms)

private static void sleepImpl(long millis, Closure closure) {
    long start = System.currentTimeMillis();
    long rest = millis;
    long current;
    while (rest > 0) {
        try {
            Thread.sleep(rest);
            rest = 0;
        } catch (InterruptedException e) {
            if (closure != null) {
                if (DefaultTypeTransformation.castToBoolean(closure.call(e))) {
                    return;
                }
            }
            current = System.currentTimeMillis(); // compensate for closure's time
            rest = millis + start - current;
        }
    }
}