Skip to content

Commit

Permalink
using ThreadLocal to make the hashmap cachedScriptEngines thread safe (
Browse files Browse the repository at this point in the history
…#398)

Co-authored-by: PickavetAndreas <[email protected]>
  • Loading branch information
PickavetAndreas and PickavetAndreas authored Oct 16, 2024
1 parent f992a8c commit 5de3aee
Showing 1 changed file with 2 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ScriptEvaluator {

private final ScriptEngineManager scriptEngineManager = new ScriptEngineManager();

private final Map<String, ScriptEngine> cachedScriptEngines = new HashMap<>();
private final ThreadLocal<Map<String, ScriptEngine>> cachedScriptEngines = ThreadLocal.withInitial(HashMap::new);
private final Map<String, ScriptEvaluatorExtension> scriptEvaluatorExtensions = new HashMap<>();

public ScriptEvaluator() {
Expand Down Expand Up @@ -62,7 +62,7 @@ private Object evalWithScriptEngine(
String language, String script, Map<String, Object> variables) {
try {
final ScriptEngine scriptEngine =
cachedScriptEngines.computeIfAbsent(language, scriptEngineManager::getEngineByName);
cachedScriptEngines.get().computeIfAbsent(language, scriptEngineManager::getEngineByName);
if (scriptEngine == null) {
final String msg = String.format("No script engine found with name '%s'", language);
throw new RuntimeException(msg);
Expand Down

0 comments on commit 5de3aee

Please sign in to comment.