1

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.

  1. Whether engine in class A will be shared with all threads or each will have separate engine objects?
  2. 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?
  3. 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.
  4. 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?
Kalai selvan
  • 63
  • 10
  • Possible duplicate of [Should I use a separate ScriptEngine and CompiledScript instances per each thread?](https://stackoverflow.com/questions/30140103/should-i-use-a-separate-scriptengine-and-compiledscript-instances-per-each-threa) – Alexey Subach Jan 17 '18 at 21:45

0 Answers0