我在 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.
有没有更好的方法来实现这一点,或者我可以让这些沙箱选项起作用?