简体   繁体   English

在Java 8 Nashorn JavaScript引擎中同时加载脚本是否有意义

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

Does it make sense to load scripts concurrently in Java 8 Nashorn JavaScript engine for faster startup? 在Java 8 Nashorn JavaScript引擎中同时加载脚本以加快启动是否有意义? Will it rize any problems even if scripts do not modify global variables? 即使脚本不修改全局变量,它会不会出现任何问题? I didn't find any information in javax.script.ScriptEngine javadocs. 我在javax.script.ScriptEngine javadocs中找不到任何信息。

Moreover, can Nashorn itself load scripts in parallel when engine.eval(...) is called from multiple threads at the same time? 此外,当同时从多个线程调用engine.eval(...)时,Nashorn本身可以并行加载脚本吗? Is it safe to do so? 这样做是否安全? If it doesn't, the whole idea of adding parallelism to scripts loading process is doomed. 如果没有,那么在脚本加载过程中添加并行性的整个想法就注定了。

It may be a good practice to compile your script files (lazily or eagerly) as CompiledScript then eval them. 将编译脚本文件(懒惰地或热切地)编译为CompiledScript然后评估它们可能是一个好习惯。

Here are example codes: https://github.com/xqbase/util/tree/master/src/main/java/com/xqbase/util/script 以下是示例代码: 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. Nashorn不是线程安全的,以及许多JS引擎,如v8。 If global variables are thread safe, however, calling CompiledScript.eval() concurrently seems no problems. 但是,如果全局变量是线程安全的,那么同时调用CompiledScript.eval()似乎没有问题。

See anther question Java Scripting With Nashorn (JSR 223) & Pre-compilation 请参阅另一个问题Java Scripting With Nashorn(JSR 223)和预编译

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM