I have two classes like below.
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.
}
}
I am new to multi-threading concept. So I have few doubts regarding this.
- Whether engine in class A will be shared with all threads or each will have separate engine objects?
- Whether engine in class A and class B will be the same? i.e. Whether both share same engine running? If yes, will they share same context too?
- If I don't change any internal variable's value after engine.eval("file.js"), can I assume it as thread-safe as long as only function is invoked? No add/remove/modify of global variables will be done further to the engine in class A.
- If I call method1 in class A from multiple threads, will it execute simultaneously/concurrently or the engine will process only one call at a time?