Anyone can clarify whether NashornScriptEngine is thread-safe or not? If it is not thread-safe, how to handle multiple scripts?
So, it's considered to be thread-safe and can be safely called by multiple threads at the same time. All threads can safely call the factorial() method and will get the expected result without interfering with each other and without altering the output that the method generates for other threads.
Variables are localized so that each thread has its own private copy. These variables retain their values across subroutine and other code boundaries and are thread-safe since they are local to each thread, even though the code which accesses them might be executed simultaneously by another thread.
The engine itself is thread-safe but the answer isn't a simple yes/no so keep on reading.
Please see this answer from someone with excellent credentials on this topic.
I'm not going to copy/paste the entire answer, but it starts off with this:
You can share a ScriptEngine and CompiledScript objects across threads. They are threadsafe. Actually, you should share them, as a single engine instance is a holder for a class cache and for JavaScript objects' hidden classes, so by having only one you cut down on repeated compilation.
What you can't share is Bindings objects ...
Make sure to read the rest of the answer!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With