NOTE
This question is not whether or not about whether or not ScriptEngine is thread safe (which I've read that it is), but it is about the actual code used to evaluate a String, not a compiled(pre-determined) function.
Background
I have written a component that evaluates a boolean expression made up of variables. For example, if a user provides (a && b || c)
, I replace the variables a
, b
, c
with true or false depending on what the variables evaluate to and then I'm using the eval()
to evaluate the boolean expression which is effectively a String parameter that is passed in.
Problem Statement
As seen in the attached image, in a multithreaded environment, the Monitor Class jdk.nashorn.internal.runtime.Context
is causing my threads which each want to evaluate a boolean expression to block on this monitor.
Question
Is there a better approach that would allow me to leverage this script engine as a boolean expression evaluator in a multithreaded environment and not cause threads to block?
Simplified Code Snippet
private static final ScriptEngineManager mgr = new ScriptEngineManager();
private static final ScriptEngine jsEngine =mgr.getEngineByName("JavaScript");
...
result = (Boolean) jsEngine.eval(expressionAsStr);
...