我有两个类,如下所示。
public class A {
private static ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
private static Invocable invoker = null;
private static Object obj = null;
static {
engine.eval("file.js");
invoker = (Invocable) engine; // Will not be changed again
obj = engine.get("ObjectName"); // Will not be changed again
// Exceptions catching goes here
}
public static String method1(String[] args) {
return invoker.invokeMethod(obj, "getValue", args[0], args[1]);
}
// More methods like this
}
public class B {
public static Object methodB() {
ScriptEngine engine = // New engine declaration same as above.
return engine.eval("Some js string + function"); // changes dynamically.
}
}
我是多线程概念的新手。所以我对此几乎没有疑问。