停止Groovy脚本的执行 [英] Stopping the execution of a Groovy script

查看:1287
本文介绍了停止Groovy脚本的执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



任何想法?谢谢。

  import groovy.lang.Binding; 
导入groovy.lang.GroovyShell;
导入groovy.transform.ThreadInterrupt;
导入org.codehaus.groovy.control.CompilerConfiguration;
导入org.codehaus.groovy.control.customizers.ASTTransformationCustomizer;

public class GroovyTest extends Thread {
private Binding binding;
私有GroovyShell外壳;

public GroovyTest(){
CompilerConfiguration compilerConfig = new CompilerConfiguration();
compilerConfig.addCompilationCustomizers(
ASTTransformationCustomizer(ThreadInterrupt.class));

binding = new Binding();

shell = new GroovyShell(this.getClass()。getClassLoader(),binding,compilerConfig);
}

@Override
public void run(){
System.out.println(Started);
$ b 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());


$ / code $ / pre

解决方案

回答我自己的问题。
即使您试图在没有关闭的情况下,Groovy的静态方法sleep也不会中断。
如果你问我,很奇怪的默认值。
推荐的方法是调用Thread.sleep(ms)

pre $ 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(); //补偿关闭时间
rest = millis + start - current;
}
}
}


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.

Any ideas? Thank you.

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

解决方案

Answering my own question. Groovy's static method sleep doesn't interrupt even if you try to if there isn't a closure. Pretty weird default if you ask me. Recomended way is to call 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;
        }
    }
}

这篇关于停止Groovy脚本的执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆