【发布时间】:2019-06-29 05:10:56
【问题描述】:
我想使用 JRuby 来运行 Ruby 脚本。
但是,我希望如果脚本花费的时间超过t 秒,它会自动关闭。
这是我的尝试:
ScriptingContainer ruby = new ScriptingContainer();
int t = 3;
new Thread() {
@Override
public void run() {
try {
Thread.sleep(t * 1000); // Timeout
System.out.println("Timeout passed.");
ruby.terminate(); // This has no effect?
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
Object output = ruby.runScriptlet(scriptWithAnInfiniteLoop);
ruby.terminate(); // Terminate without timeout, at the end of the script
【问题讨论】:
标签: java ruby multithreading jruby thread-sleep