在Java 8 Nashorn JavaScript引擎中并发加载脚本是有意义的 [英] Does it make sense to load scripts concurrently in Java 8 Nashorn JavaScript engine

查看:1841
本文介绍了在Java 8 Nashorn JavaScript引擎中并发加载脚本是有意义的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将脚本并发加载到Java 8 Nashorn JavaScript引擎中是否有意义,以便更快地启动?即使脚本不修改全局变量,它会破坏任何问题吗?我在 javax.script.ScriptEngine javadocs中找不到任何信息。

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.

此外,Nashorn本身可以加载脚本并行时,同时从多个线程调用 engine.eval(...)?这样做是否安全?

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.

推荐答案

这可能是一个很好的习惯

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

这里是示例代码: https://github.com/xqbase/util/tree/master/src/main/java/com / xqbase / util / script

Nashorn不是线程安全的,还有许多像v8这样的JS引擎。如果全局变量是线程安全的,但是,同时调用CompiledScript.eval()似乎没有问题。

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.

查看另一个问题 Java脚本使用Nashorn(JSR 223)&预编译

这篇关于在Java 8 Nashorn JavaScript引擎中并发加载脚本是有意义的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆