问题描述
我在 graalVM 文档中找到了沙盒选项作为设置 sandBox.MaxcpuTime 的一种方式,以限制线程运行的时间 - https://www.graalvm.org/reference-manual/embed-languages/
我已经尝试了以下代码 -
try (Context context = Context.newBuilder("js")
.allowExperimentalOptions(true)
.option("sandBox.MaxcpuTime","10s")
.option("sandBox.MaxcpuTimeCheckInterval","5ms")
.build())
{
try {
context.eval("js","while(true);");
assert false;
} catch (polyglotException e) {
// Triggered after 500ms;
// Context is closed and can no longer be used
// Error message: Maximum cpu time limit of 500ms exceeded.
assert e.isCancelled();
assert e.isResourceExhausted();
}
context.close(true);
}
这对我来说一直失败并出现错误 -
java.lang.IllegalArgumentException: Could not find option with name sandBox.MaxcpuTime.
有没有更好的方法来实现这一点,或者我可以让这些沙箱选项发挥作用?
解决方法
您可能想要使用更通用的解决方案,它可能与其他脚本引擎(例如 rhino 或 nashorn)一起使用,而不管内置功能如何:
final ExecutorService executor = Executors.newSingleThreadExecutor();
final Context context = Context.newBuilder("js").build();
final Future<Object> futureResult = executor.submit(() -> context.eval("js","while(true);"));
try {
final Object result = futureResult.get(10,TimeUnit.SECONDS);
System.out.println("Script evaluated within 10 seconds,result: " + result);
} catch (TimeoutException e) {
context.interrupt(Duration.ZERO);
System.out.println("Script not evaluated within 10 seconds,interrupted.");
}
System.out.println("Done.");
此解决方案的另一个优点是它允许您使用线程池,从而使您可以更好地控制并发脚本的执行方式(例如,您可以将同时执行的脚本数量限制为一个,或者到可用 CPU 内核的数量等)。
但请注意,示例中指定的时间限制是自提交任务以来经过的时间,而不是实际执行给定脚本所花费的时间。