我的ScriptExecutor出现问题,这被多个线程调用。我不希望在两个不同线程中执行的两个脚本之间有任何干扰(所谓干扰,至少是指全局变量)
@Service
public class ScriptExecutor
{
private ScriptEngine engine;
public ScriptExecutor()
{
System.setProperty("org.jruby.embed.localcontext.scope", "threadsafe");
engine = new ScriptEngineManager().getEngineByName("jruby");
engine.getContext().setWriter(new OutputStreamWriter(...));
engine.getContext().setErrorWriter(new OutputStreamWriter(...));
}
public Object executeScript(String path, Object args){
{
//... Load script from path
engine.eval(r);
return (Invocable) engine.invokeFunction("myFunction", args);
}
}
此实现线程安全吗?我的意思是: