Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is NashornScriptEngine thread-safe? [duplicate]

Anyone can clarify whether NashornScriptEngine is thread-safe or not? If it is not thread-safe, how to handle multiple scripts?

like image 493
newtonash Avatar asked Oct 11 '15 09:10

newtonash


People also ask

Is multi threaded thread-safe?

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.

Is variable thread-safe?

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.


1 Answers

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!

like image 159
olafure Avatar answered Sep 20 '22 15:09

olafure