Rhino和并发访问javax.script.ScriptEngine [英] Rhino and concurrent access to javax.script.ScriptEngine

查看:665
本文介绍了Rhino和并发访问javax.script.ScriptEngine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 javax.script API使用Rhino 1.6r2。我知道Rhino引擎声称是 MULTITHREADED:引擎实现是内部线程安全的,脚本可以并发执行,尽管脚本执行对一个线程的影响可能对其他线程上的脚本可见。

I'm using Rhino 1.6r2 through the javax.script API. I know that the Rhino engine claims to be MULTITHREADED: "The engine implementation is internally thread-safe and scripts may execute concurrently although effects of script execution on one thread may be visible to scripts on other threads."

我想知道的是,在什么条件下,一个脚本执行的效果对另一个脚本执行的影响是什么?在我的代码中,我有时会重用 ScriptEngine 对象,但是对于每次执行,我都会创建一个新的 SimpleBindings 并传递它到 eval(String,Bindings)。有了这种安排,内部状态是否有任何方式可以从一个执行泄漏到另一个执行?如果是这样,怎么样?

What I'd like to know is, under what exact conditions would the effects of one script execution be visible to another? In my code, I sometimes re-use a ScriptEngine object, but for every execution I create a new SimpleBindings and pass it to eval(String, Bindings). With this arrangement, is there any way that internal state could leak from one execution to another? If so, how?

这里有一个非常翔实的答案 ,但它相当告诉我我需要知道的事情。

There's a very informative answer here, but it doesn't quite tell me what I need to know.

推荐答案

是的,JSR223未指定脚本语言中的变量应如何与给定的 Bindings 绑定。因此,实施者完全有可能选择在引擎实例中存储全局范围变量,并在评估脚本时使用不同的 Bindings 重用它。

Yes, JSR223 didn't specify how variables in script language should be bound with given Bindings. Therefore it is totally possible the implementers choose storing global scope variables in engine instance and reuse it even given different Bindings when evaluating script.

例如,JRuby的JSR223绑定有一种模式以这种方式工作

For example, JRuby's JSR223 binding has one mode working in this way

import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleScriptContext;

public class Jsr223Binding {


    private Jsr223Binding() throws ScriptException {
        System.setProperty("org.jruby.embed.localvariable.behavior", "transient");
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("jruby");
        ScriptContext ctx1 = new SimpleScriptContext();
        ScriptContext ctx2 = new SimpleScriptContext();
        engine.eval("$foo = 5\nputs $foo", ctx1);
        engine.eval("puts $foo", ctx2);
    }

    public static void main(String[] args) throws ScriptException {
        new Jsr223Binding();
    }
}

这篇关于Rhino和并发访问javax.script.ScriptEngine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆