Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 停止Groovy脚本的执行_Java_Multithreading_Groovy_Groovyshell - Fatal编程技术网

Java 停止Groovy脚本的执行

Java 停止Groovy脚本的执行,java,multithreading,groovy,groovyshell,Java,Multithreading,Groovy,Groovyshell,我正在我的代码中嵌入Groovy运行时,我希望能够中断它。我无法控制将要运行的脚本。我读过groovy.transform.ThreadInterrupt来处理线程中断,但由于某些原因,下面的代码无法正常工作。它实际上在等待10000毫秒,而不是1000毫秒,在那个里它应该被中断 有什么想法吗?多谢各位 import groovy.lang.Binding; import groovy.lang.GroovyShell; import groovy.transform.ThreadInterru

我正在我的代码中嵌入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());
    }
}
导入groovy.lang.Binding;
导入groovy.lang.GroovyShell;
导入groovy.transform.ThreadInterrupt;
导入org.codehaus.groovy.control.CompilerConfiguration;
导入org.codehaus.groovy.control.customizers.ASTTransformationCustomizer;
公共类GroovyTest扩展线程{
私人约束;
私有GroovyShell外壳;
公共GroovyTest(){
CompilerConfiguration CompilerConfiguration=新建CompilerConfiguration();
compilerConfig.AddCompilerCustomizers(
新的ASTTransformationCustomizer(ThreadInterrupt.class));
绑定=新绑定();
shell=newGroovyShell(this.getClass().getClassLoader(),binding,compilerConfig);
}
@凌驾
公开募捐{
System.out.println(“已启动”);
run(“for(inti=0;i<10;i++){sleep(1000)}”,“test”,新字符串[]{});
系统输出打印项次(“完成”);
}
公共静态void main(字符串args[])引发InterruptedException{
GroovyTest=新的GroovyTest();
test.start();
System.out.println(“休眠:+System.currentTimeMillis());
睡眠(1000);
System.out.println(“中断:+System.currentTimeMillis());
test.interrupt();
test.join();
System.out.println(“中断?:”+System.currentTimeMillis());
}
}
回答我自己的问题。 Groovy的静态方法sleep不会中断,即使在没有闭包的情况下尝试中断。 如果你问我的话,很奇怪。 建议的方法是调用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;
        }
    }
}