java 停止执行_java-停止执行Groovy脚本

我正在代码中嵌入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());

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值