Rhino脚本引擎的生命周期和并发语义是什么? [英] What is the lifecycle and concurrency semantics of Rhino Script Engine

查看:368
本文介绍了Rhino脚本引擎的生命周期和并发语义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对(Rhino)脚本引擎和相关类的生命周期和并发语义感兴趣。具体来说:

I am interested in the lifecycle and concurrency semantics of (Rhino) Script Engine and associated classes. Specifically:


  1. Bindings 应该是线程安全的吗?

  2. 是否应允许多个线程共享一个ScriptEngine实例?

  3. ...或者每个线程应构建一个短期实例?
  4. $ b $ 如果多个线程同时调用 ScriptEngine.eval(...)
  5. 针对 CompiledScript 实例的相同问题

  6. 使用 Invocable.getInterface(...)

  7. 大概,放在Bindings中的对象遵循Java的垃圾回收。

  1. Is Bindings supposed to be thread safe?
  2. Should multiple threads be allowed to share a single ScriptEngine instance?
  3. ... or should each thread construct a short-lived instance?
  4. ... or keep them in a pool?
  5. What happens if multiple threads concurrently call ScriptEngine.eval(...)?
  6. Same questions for CompiledScript instances
  7. Same questions for interface implementations generated using Invocable.getInterface(...)?
  8. Presumably, objects placed in Bindings follow Java's garbage collection. What about garbage collection of objects that don't end up in the bindings?


推荐答案

那么,我运行的实验和Rhino引擎报告Mozilla Rhino是JavaDocs断言的MULTITHREADED。

So I've run the experiment and the Rhino engine reports "Mozilla Rhino" is MULTITHREADED which the JavaDocs asserts


MULTITHREADED是内部线程安全的
和脚本可以同时执行,尽管脚本
执行在一个线程的效果可能对其他线程上的脚本可见。

"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."

这里是代码...它对我来说线程安全,只要你传递的绑定是线程安全的。

Here's the code...it looks threadsafe to me, as long as the bindings you pass in are threadsafe too.

package org.rekdev;
import java.util.*;
import javax.script.*;
public class JavaScriptWTF {
    public static void main( String[] args ) {
        ScriptEngineManager mgr = new ScriptEngineManager();
        List<ScriptEngineFactory> factories = mgr.getEngineFactories();
        for ( ScriptEngineFactory factory : factories ) {
            System.out.println( String.format(
                    "engineName: %s, THREADING: %s",
                    factory.getEngineName(),
                    factory.getParameter( "THREADING" ) ) );
        }
    }
}

... ...


engineName:AppleScriptEngine,THREADING:null

engineName:Mozilla Rhino,THREADING:MULTITHREADED

engineName: AppleScriptEngine, THREADING: null
engineName: Mozilla Rhino, THREADING: MULTITHREADED

要回答您的确切问题...

To answer your exact question...


  1. Bindings是否应该是线程安全的?

    我觉得这是你的责任,让他们线程安全。换句话说,只传递不可变对象,以及引擎是否是线程安全的都不是问题。

  1. Is Bindings supposed to be thread safe?
    It sounds to me that it is your responsibility to make them Thread-safe. In other words, pass in only immutable objects and whether the engine is Thread-safe or not becomes a non-issue.

如果允许多个线程共享一个ScriptEngine实例?

这听起来像他们可以,但关键是状态共享可以通过绑定发生。不可变对象是你的朋友。

Should multiple threads be allowed to share a single ScriptEngine instance?
It sounds to me like they can, but the key is the state sharing that can occur through the Bindings. Immutable objects are your friend.

...或者每个线程都应该构造一个短暂的实例?那么最好的方法就是每次执行eval都是一个短暂的实例。

...or should each thread construct a short-lived instance?
It seems to me that the best way to think of this is that each execution of eval is a short lived instance.

...或者将它们保存在池中? />
在这个试图自己池资源的日子很少是一个好主意。

... or keep them in a pool?
In this day and age attempting to pool resources on your own is rarely a good idea. Give the short-lived instance a shot, measure its performance, and work out from there.

如果多个线程同时调用ScriptEngine.eval(..),会发生什么? 。)
如果我理解Rhino引擎对MULTITHREADING的响应正确,ScriptEngine.eval应该对并发调用很好。

What happens if multiple threads concurrently call ScriptEngine.eval(...)?
If I understand the Rhino engine's repsonse to MULTITHREADING correctly, ScriptEngine.eval should be fine with concurrent calls.

CompiledScript实例的相同问题
http://docs.oracle.com/javase/6/docs/api/ javax / script / CompiledScript.html 。因此,在您看起来试图最小化ScriptEngine实例数的环境中,它们不会声音线程安全。

Same question for CompiledScript instances
The JavaDocs state that "Changes in the state of the ScriptEngine caused by execution of the CompiledScript may visible during subsequent executions of scripts by the engine." http://docs.oracle.com/javase/6/docs/api/javax/script/CompiledScript.html. So they don't sound Thread-safe at all in an environment where you appear to be trying to minimize the number of ScriptEngine instances.

接口的相同问题使用Invocable.getInterface(...)生成的实现?
你在这里你自己。我不明白为什么或什么时候使用这种能力,它听起来像我一样,你可能在这里跳鲨鱼。如果你想深入到脚本语言,我建议你放弃JavaScript,看看Groovy一个更可脚本的Java。

Same questions for interface implementations generated using Invocable.getInterface(...)? You are on your own here. I don't understand exactly why or when this capability would be used and it sounds to me like you may be "jumping the shark" here. If you want to go this deep into the scripting language, I recommend that you abandon JavaScript and look at Groovy for a more scriptable Java.

在绑定遵循Java的垃圾收集。那么垃圾收集的对象不会在绑定中结束呢??
如果他们没有以绑定结束,我期望他们绑定到ScriptEngine,并遵循它的生命周期(基于我读过的文档)。合并ScriptEngine实例听起来不是一个好主意。

Presumably, objects placed in Bindings follow Java's garbage collection. What about garbage collection of objects that don't end up in the bindings?
If they don't end up in bindings I expect them to be bound to the ScriptEngine and follow its lifecycle (based upon the docs that I have read). Pooling the ScriptEngine instances does not sound like a great idea.

这篇关于Rhino脚本引擎的生命周期和并发语义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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