Does it make sense to load scripts concurrently in Java 8 Nashorn JavaScript engine

半腔热情 提交于 2019-12-10 20:55:38

问题


Does it make sense to load scripts concurrently in Java 8 Nashorn JavaScript engine for faster startup? Will it rize any problems even if scripts do not modify global variables? I didn't find any information in javax.script.ScriptEngine javadocs.

Moreover, can Nashorn itself load scripts in parallel when engine.eval(...) is called from multiple threads at the same time? Is it safe to do so? If it doesn't, the whole idea of adding parallelism to scripts loading process is doomed.


回答1:


It may be a good practice to compile your script files (lazily or eagerly) as CompiledScript then eval them.

Here are example codes: https://github.com/xqbase/util/tree/master/src/main/java/com/xqbase/util/script

Nashorn is not thread safe, as well as many JS engines like v8. If global variables are thread safe, however, calling CompiledScript.eval() concurrently seems no problems.

See anther question Java Scripting With Nashorn (JSR 223) & Pre-compilation



来源:https://stackoverflow.com/questions/27032656/does-it-make-sense-to-load-scripts-concurrently-in-java-8-nashorn-javascript-eng

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!