Остановка выполнения скрипта Groovy - PullRequest
3 голосов
/ 12 августа 2011

Я внедряю Groovy runtime в свой код и хотел бы иметь возможность его прерывать. У меня нет контроля над сценариями, которые будут запускаться. Я читал о 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 Ответ

4 голосов
/ 13 августа 2011

Отвечая на мой собственный вопрос.Статический метод Groovy sleep не прерывает, даже если вы пытаетесь, если нет замыкания.Довольно странный дефолт, если вы спросите меня.Рекомендуемый способ - вызвать Thread.sleep (мс)

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;
        }
    }
}
.
...